【数学】 2^x mod n = 1

描述

Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.

输入

One positive integer on each line, the value of n.

输出

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.

样例输入

2
5

样例输出

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

题目来源

ZOJ Monthly 2003.2
分析:
数论-乘法逆元。
2的n次 mod N=1 等同于GCD(2^n,N)=1
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,ans,t;
while(cin>>n)
{
if (n1||n%20)
{
cout<<“2^? mod “<<n<<” = 1”<<endl;
}
else
{
ans=2;
t=1;
while(ans!=1)
{
ans=ans*2%n;
t++;
}
cout<<“2^”<<t<<" mod “<<n<<” = 1"<<endl;
}
}
return 0;
}

发布了122 篇原创文章 · 获赞 0 · 访问量 4677

猜你喜欢

转载自blog.csdn.net/Skynamer/article/details/104130689
今日推荐