HDOJ 2^x mod n = 1 1395

来源:互联网 发布:usb端口上的电涌不足 编辑:程序博客网 时间:2024/05/16 13:49

2^x mod n = 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13630    Accepted Submission(s): 4213


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
25
 

Sample Output
2^? mod 2 = 12^4 mod 5 = 1
 

Author
MA, Xiao
 

Source
ZOJ Monthly, February 2003
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  2462 2421 2521 1695 2466 


#include<stdio.h>int F_pow(int x,int n,int modn){int res=1;while(n){    if(n&1) res=res*x%modn;    x=x*x%modn;    n>>=1;}    return res;}int main(){int F_pow(int x,int n,int modn);int n;while(scanf("%d",&n)!=EOF){int i=0;if(!(n&1)||n<=1){printf("2^? mod %d = 1\n",n);continue;}int num=0;for(i=1;;i++){num=F_pow(2,i,n);if(num==1) break;}printf("2^%d mod %d = 1\n",i,n);}return 0;}


 
0 0