您的位置:首页 > 其它

HD_1395 2^x mod n = 1

2015-12-10 19:36 246 查看


2^x mod n = 1

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14852 Accepted Submission(s): 4576



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


很水的一道题,因为是对所输入的数求余,因此保存余数与保存原数得到的结果是一样所以说这样可以解决因数据量大而造成的超时的问题,也就是说每次只保存每次取余后的值即:a=a%n;这样数据量可以减少,程序运行的时间就会减少。代码如下:

#include<stdio.h>

int main(){
int n,k,a;
while(scanf("%d",&n) == 1){
if( n%2 == 0 || n==1)
printf("2^? mod %d = 1\n",n);
else{
k=1,a=2;
while(k++){
a*=2;
a%=n;
if(a==1)
break;
}
printf("2^%d mod %d = 1\n",k,n);
}
//printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: