标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1071 Accepted Submission(s): 323
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <stdlib.h> #include <math.h> using namespace std; typedef long long LL; LL e[100][2]; LL phi(LL x) { LL ans=x; for(LL i=2; i*i<=x; i++) if(x%i==0) { ans=ans/i*(i-1); while(x%i==0) x/=i; } if(x>1) ans=ans/x*(x-1); return ans; } LL gcd(LL a,LL b) { return b==0?a:gcd(b,a%b); } LL pow_mod(LL a,LL n,LL mod) { LL ans = 1; while(n) { if(n&1) ans=ans*a%mod; a=a*a%mod; n>>=1; } return ans; } void devide(LL ans,int &id) { for(LL i=2; i*i<=ans; i++) ///分解质因数 { if(ans%i==0) { e[id][0]=i; e[id][1]=0; while(ans%i==0) ans/=i,e[id][1]++; id++; } } if(ans>1) { e[id][0]=ans; e[id++][1]=1; } } int main() { LL X,Y,a0; while(~scanf("%lld%lld%lld",&X,&Y,&a0)) { Y = Y/(X-1); LL d = gcd(Y,a0); a0 = a0/d; if(gcd(X,a0)!=1) { printf("Impossible!\n"); } else { LL ans = phi(a0); int id = 0; devide(ans,id); for(int i=0; i<id; i++) { for(int j=0; j<e[i][1]; j++) { if(pow_mod(X,ans/e[i][0],a0)==1) ans/=e[i][0]; ///分解本身,得到 X^ans % a0 = 1的最小ans } } printf("%lld\n",ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5535925.html