如何通过暴力枚举解决HDU 1395中2^x mod n = 1问题?

2026-06-10 03:511阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计623个文字,预计阅读时间需要3分钟。

如何通过暴力枚举解决HDU 1395中2^x mod n = 1问题?

给定一个数字 n,找到满足 \(2^x \mod n=1\) 的最小正整数 \(x\)。时间限制:2000/1000 MS(Java/其他)内存限制:65536/32768 K(Java/其他)总提交数:15721通过提交数:4870

问题描述:给定一个正整数 \( n \),求解方程 \(2^x \mod n=1\) 的最小正整数解 \( x \)。


2^x mod n = 1

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

Problem Description

如何通过暴力枚举解决HDU 1395中2^x mod n = 1问题?

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

Author

MA, Xiao

Source

​​ZOJ Monthly, February 2003 ​​

题解:暴力枚举....

TLE代码:

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<algorithm>
#include<time.h>
typedef long long LL;
using namespace std;
int main()
{
int x,n;
while(cin>>n)
{
int k=0;
for(int i=1;i<=5000;i++)
{
if((int)pow(2,(double)i)%n==1){
printf("2^%d mod %d = 1\n",i,n);break;
}

k++;
}
if(k==5000)
cout<<"2^? mod "<<n<<" = 1"<<endl;

}

return 0;
}



#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<algorithm>
#include<time.h>
typedef long long LL;
using namespace std;
int main()
{

int k,m,x,n;
while(cin>>n)
{
k=1;
x=2;
if(n%2==0||n==1)
cout<<"2^? mod "<<n<<" = 1"<<endl;
else
{
while(k++)
{

x*=2;
x=x%n;
if(x==1)
break;
}
cout<<"2^"<<k<<" mod "<<n<<" = 1"<<endl;
}
}
return 0;
}



本文共计623个文字,预计阅读时间需要3分钟。

如何通过暴力枚举解决HDU 1395中2^x mod n = 1问题?

给定一个数字 n,找到满足 \(2^x \mod n=1\) 的最小正整数 \(x\)。时间限制:2000/1000 MS(Java/其他)内存限制:65536/32768 K(Java/其他)总提交数:15721通过提交数:4870

问题描述:给定一个正整数 \( n \),求解方程 \(2^x \mod n=1\) 的最小正整数解 \( x \)。


2^x mod n = 1

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

Problem Description

如何通过暴力枚举解决HDU 1395中2^x mod n = 1问题?

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

Author

MA, Xiao

Source

​​ZOJ Monthly, February 2003 ​​

题解:暴力枚举....

TLE代码:

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<algorithm>
#include<time.h>
typedef long long LL;
using namespace std;
int main()
{
int x,n;
while(cin>>n)
{
int k=0;
for(int i=1;i<=5000;i++)
{
if((int)pow(2,(double)i)%n==1){
printf("2^%d mod %d = 1\n",i,n);break;
}

k++;
}
if(k==5000)
cout<<"2^? mod "<<n<<" = 1"<<endl;

}

return 0;
}



#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<algorithm>
#include<time.h>
typedef long long LL;
using namespace std;
int main()
{

int k,m,x,n;
while(cin>>n)
{
k=1;
x=2;
if(n%2==0||n==1)
cout<<"2^? mod "<<n<<" = 1"<<endl;
else
{
while(k++)
{

x*=2;
x=x%n;
if(x==1)
break;
}
cout<<"2^"<<k<<" mod "<<n<<" = 1"<<endl;
}
}
return 0;
}