2^x mod n = 1
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10564 Accepted Submission(s): 3267
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.
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
数论题又水了,还是要多做才能积累
!!
分析:
1、n==1或者n%2==0,都不会有这样的2的幂次存在。
因为2^k(k=1、2、3...)为偶数,n为偶数时显然不存在;n==1则容易验证。
2、n为奇数是则一定存在。
n为奇数,则至少会存在一个偶数模取n等于1。2^k则会找到所有的偶数。
3、2^k%n=(2^a*2^b)%n=((2^a%n)*(2^b%n))%n。(a+b=k)
AC的代码:
#include<stdio.h>
int main()
{
int n,s,temp;
while (~scanf("%d",&n))
{
if (n==1||n%2==0)
{
printf("2^? mod %d = 1\n",n);
}
else
{
s=1,temp=2;
while (temp!=1)
{
temp=temp*2%n;
s++;
}
printf("2^%d mod %d = 1\n",s,n);
}
}
return 0;
}