# [SDOI2011]计算器

### Description

1、给定y,z,p,计算Y^Z Mod P 的值；
2、给定y,z,p，计算满足xy≡ Z ( mod P )的最小非负整数；
3、给定y,z,p，计算满足Y^x ≡ Z ( mod P)的最小非负整数。

【样例输入1】

3 1

2 1 3

2 2 3

2 3 3

【样例输入2】

3 2

2 1 3

2 2 3

2 3 3

【数据规模和约定】

Sample Output
【样例输出1】

2

1

2

【样例输出2】

2

1

0

`c++

//我已经无所畏惧了！

# include<bits/stdc++.h>

using namespace std;
int T;
long long y, z, p;

inline long long fpow(long long a, long long t){
long long ret = 1, tmp = a;
while(t){
if(t & 1) ret = ret * tmp % p;
tmp = tmp * tmp % p; t >>= 1;
}
return ret;
}

namespace W1{
inline void workk(){
while(T--){
scanf("%lld%lld%lld", &y, &z, &p);
printf("%lld\n", fpow(y, z));
}
}
}

namespace W2{
inline void workk(){
while(T--){
scanf("%lld%lld%lld", &y, &z, &p); z %= p; y %= p;
if(!z){printf("%lld\n", (y % p == 0) ? 1 : p); continue;}
if(!y){printf("Orz, I cannot find x!\n"); continue;}
long long x = fpow(y, p - 2); x *= z;
printf("%lld\n", x % p);
}
}
}

namespace W3{
inline void workk(){
while(T--){
scanf("%lld%lld%lld", &y, &z, &p); long long ans = p; z %= p; y %= p;
if(!z){
if(!y){printf("1\n"); continue;}
printf("Orz, I cannot find x!\n"); continue;
}
if(z == 1){printf("0\n"); continue;}
if(!y){printf("Orz, I cannot find x!\n"); continue;}
long long sq = sqrt(p); map<long long, long long> mp;
for(int i = 0; i < sq; ++i) mp[z * fpow(fpow(y, i), p - 2) % p] = i + 1;
int f = 0; long long lim = p / sq;
for(int i = 0; i <= lim; ++i){
long long now = fpow(y, i * sq);
long long x = mp[now]; if(!x) continue;
printf("%lld\n", i * sq + x - 1); f = 1; break;
}
if(f) continue;
printf("Orz, I cannot find x!\n");
}
}
}

int main()
{
int opt; scanf("%d%d", &T, &opt);
if(opt == 1) W1::workk();
if(opt == 2) W2::workk();
if(opt == 3) W3::workk();
return 0;
}

posted @ 2018-09-30 16:34  沛霖  阅读(131)  评论(0编辑  收藏  举报