标签:des style blog http io ar os sp java
ZOJ Monthly, February 2003
题目大意:给你一个数N,判断是否存在x,满足2^x mod N = 1。若
满足,对于满足条件的最小x,输出2^x mod N = 1,否则输出
2^? mod 2 = 1。
思路:用到数论上的乘法逆元的规律了。
乘法逆元:对于整数a、p如果存在整数b,满足a*b mod p = 1,则称
b是a的模p的乘法逆元。a存在模p的乘法逆元的充要条件是gcd(a,p) = 1
此题中,令a = 2^x,b = 1,p = n,则若存在x使得2^x mod N = 1,
则gcd(2^x,N) = 1。
1>.因为N>0,当N为偶数时,gcd(2^x,N) = 2*k(k=1,2,3……),不满足
2>.当N为奇数时,gcd(2^x,N) = 1满足条件。
3>.当N为1时,2^x mod N = 0,不符合条件
所以N为奇数,且不为1,满足2^x mod N = 1,暴力求解。
参考博文:http://blog.csdn.net/mxway/article/details/8954364
#include<stdio.h>
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==1 || !(n&1))
{
printf("2^? mod %d = 1\n",n);
}
else
{
int ans = 2,num = 1;
while(ans!=1)
{
ans = ans * 2 % n;
num++;
}
printf("2^%d mod %d = 1\n",num,n);
}
}
return 0;
}
标签:des style blog http io ar os sp java
原文地址:http://blog.csdn.net/lianai911/article/details/41548143