题目地址:
http://acm.hdu.edu.cn/showproblem.php?pid=1395
题目描述:
2^x mod n = 1
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12685 Accepted Submission(s): 3951
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
题意:
求最小的指数是公式成立。
题解:
同余定理的运用,可以认为 t 是等于 t%n的,如果涉及到乘法然后取余等运用同余的地方。
(a * b) % n = ((a % n) * b) % n (同余同取余)
代码:
#include
#define MM 10000
long long two[MM] = {0};
int x=0,n=0;
int main()
{
while(scanf("%d",&n)!=EOF)
{
int t = 1;
for(x=1;x<=MM-1;x++)
{
t*=2;
if(t%n==1) break;
else t=t%n;//sub the number 's range
}
if(x