Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13345 Accepted Submission(s): 4146
Problem Description
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
Input
One positive integer on each line, the value of n.
Output
If the minimum x exists, print a line with 2^x mod n = 1.
Print 2^? mod n = 1 otherwise.
You should replace x and n with specific numbers.
Sample Input
2
5
Sample Output
2^? mod 2 = 1
2^4 mod 5 = 1
Author
MA, Xiao
#include<cstdio> #include<cmath> int Powermod(int a,int b,int c)//快速幂 { int ans=1; if(a%c==0) return 0; a=a%c; while(b) { if(b&1) ans=ans*a%c; a=a*a%c; b>>=1; } return ans; } int main() { int i,n; //奇数除了1一定有结果,偶数一定没结果 while(~scanf("%d",&n)) { if(n%2==0||n==1)//2^x对偶数求余结果为偶数,不为1 1的时候结果也不存在 {printf("2^? mod %d = 1\n",n);continue;} for(i=1;; i++)//对于2^x mod n,当1<=i<=n 就能得到所有求余结果 if(Powermod(2,i,n)==1) { printf("2^%d mod %d = 1\n",i,n); break; } } }
时间: 2024-12-30 03:05:53