您的位置:首页 > 其它

ACM 数论 hdu 1395 2^x mod n = 1

2016-05-12 13:53 369 查看
[align=left]Problem Description[/align]
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.

[align=left]Input[/align]
One positive integer on each line, the value of n.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2
5


[align=left]Sample Output[/align]

2^? mod 2 = 1
2^4 mod 5 = 1


这道题数据简单,暴力求解可以出来,但真要用思维做,需要用到欧拉函数,在此先不用了

但是注意,当x为1或者偶数时,是绝对不可能实现的,首先pass

其余的进行暴力求解法即可

#include<stdio.h>

int main(){
int x,n,t;
while(scanf("%d",&n)!=EOF){
if(n==1 || n%2==0)
{

printf("2^? mod %d = 1\n",n);
}
else
{

x=1;
t=2;

while(t%n!=1){
x++;
t=t*2%n; //反之数值过大

}
printf("2^%d mod %d = 1\n",x,n);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: