2^x mod n = 1
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6543 Accepted Submission(s): 1961
Print 2^? mod n = 1 otherwise.
You should replace x and n with specific numbers.
//hash的应用,判重复,
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
bool h[10000];
int main()
{
int n,t,k;
while(scanf("%d",&n)!=EOF)
{
memset(h,0,n*sizeof(bool));
k=1;t=2;h[2]=1;
while(t%n!=1)
{
k++;
t=t<<1;
t=t%n;
if(h[t]) break;
h[t]=1;
}
if(t%n!=1)
printf("2^? mod %d = 1\n",n);
else
printf("2^%d mod %d = 1\n",k,n);
}
return 0;
}
// 加进去点东西,不过貌似没加快速度呀
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
bool h[10000];
int main()
{
int n,t,k;
while(scanf("%d",&n)!=EOF)
{
if(n%2==0||n==1)//这里可以这样理解 n%2=0,n是偶数, 2^x是偶数,2^x%n也是偶数,所以不可 能 会 // 是1
{printf("2^? mod %d = 1\n",n);continue;}
memset(h,0,n*sizeof(bool));
k=1;t=2;h[2]=1;
while(t%n!=1)
{
k++;
t=t<<1;
t=t%n;
if(h[t]) break;
h[t]=1;
}
if(t%n!=1)
printf("2^? mod %d = 1\n",n);
else
printf("2^%d mod %d = 1\n",k,n);
}
return 0;
}