2242: [sdoi2011] calculator time limit: 10 sec memory limit: 512 MB
Submit: 1278 solved: 492
[Submit] [Status] Description you are required to design a calculator to complete the following three tasks: 1. Calculate the value of y ^ Z mod P for the given Y, Z, P, calculate the minimum non-negative integer that satisfies XY ≡ Z (mod p). 3. Calculate the minimum non-negative integer that satisfies y ^ x ≡ Z (mod p) given Y, Z, P. Input
The input contains multiple groups of data.
The first line contains two positive integers t, K representing the number of data groups and the query type (for all data in a test point, the query type is the same ). Each line below contains three positive integers Y, Z, and P, which describe a query. Output outputs a line of answers for each query. For query Types 2 and 3, if the conditions are not met, the output is "orz, I cannot find X !", Note that there is a space between comma and I. Sample input [Example input 1]
3 1
2 1 3
2 2 3
2 3 3
[Example input 2]
3 2
2 1 3
2 2 3
2 3 3
[Data scale and Conventions]
For 100% of data, 1 <= Y, Z, P <= 10 ^ 9, is the prime number, 1 <= T <= 10. Sample output [sample output 1]
2
1
2
[Sample output 2]
2
1
0
Hint Source
Day1 in the first round
Question: The first question is the fast power, the second question is the expansion of Euclidean, and the third question is the stride small step algorithm of shank. The algorithm is based on this property: If a ^ (K * m + I) = B (mod P), a ^ I = B/(a ^ km) = B * (a ^ km)-1 (mod p) where-1 indicates the inverse element of the modulo p. If the I on the left is very small, we can pre-process all a ^ I, and then enumerate K to check whether there is a key value such as B * (a ^ km)-1, the answer is K * m + I. obviously, it is more appropriate for m to obtain SQRT (n. When K is enumerated, there is also a ^ km inverse element with K increase, do not need to every request, we first find a ^ m inverse element T = power (A, p-m-1, P) so each time K ++, B * = T, this is because a ^ (p-m-1 + p-m-1) = a ^ (P-2 * m-1) * A ^ (p-1) = a ^ (p-m-1 ). You can store queries by map or by hand. Bzoj's evaluation machine is very touching. The local test is more than 50 s, and on bzoj, it is a miracle that one second has run out. Code:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 500+100 14 #define maxm 500+100 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 using namespace std; 22 typedef map<int,int>::const_iterator cit; 23 typedef map<int,int>::value_type vt; 24 map<int,int> mp; 25 inline ll read() 26 { 27 int x=0,f=1;char ch=getchar(); 28 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 29 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 30 return x*f; 31 } 32 ll a,b,p,x,y,t,tt,gcd,m,cs,ch; 33 ll work1(ll a,ll b,ll p) 34 { 35 ll ans=1; 36 for(;b;b>>=1,a*=a,a%=p) 37 if(b&1)ans*=a,ans%=p; 38 return ans; 39 } 40 void exgcd(ll a,ll b) 41 { 42 if(!b){x=1;y=0;gcd=a;} 43 else 44 { 45 exgcd(b,a%b); 46 t=x;x=y;y=t-(a/b)*y; 47 } 48 } 49 void work2() 50 { 51 exgcd(a,p); 52 if(b%gcd)puts("Orz, I cannot find x!"); 53 else 54 { 55 x*=b/gcd;y*=b/gcd; 56 t=p/gcd; 57 x=(x%t+t)%t; 58 printf("%lld\n",x); 59 } 60 } 61 void work3() 62 { 63 a%=p;b%=p; 64 if(!a&&!b){printf("1\n");return;} 65 if(!a){puts("Orz, I cannot find x!");return;} 66 m=floor(sqrt(p)); 67 mp.clear(); 68 mp.insert(mp.begin(),vt(1,0)); 69 t=1; 70 for1(i,m-1)t*=a,t%=p,mp.insert(mp.begin(),vt(t,i)); 71 t=work1(a,p-m-1,p); 72 for0(i,p/m) 73 { 74 cit j=mp.find(b); 75 if(j!=mp.end()) 76 { 77 printf("%lld\n",j->second+i*m); 78 return; 79 } 80 b*=t;b%=p; 81 } 82 puts("Orz, I cannot find x!"); 83 } 84 int main() 85 { 86 freopen("input.txt","r",stdin); 87 freopen("output.txt","w",stdout); 88 cs=read();ch=read(); 89 while(cs--) 90 { 91 a=read();b=read();p=read(); 92 switch(ch) 93 { 94 case 1:printf("%lld\n",work1(a,b,p));break; 95 case 2:work2();break; 96 case 3:work3();break; 97 } 98 } 99 return 0;100 }
View code
Bzoj2242: [sdoi2011] Calculator