HDU 1395 2^x mod n = 1

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


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
 

 

Author
MA, Xiao
 

 

Source
 

 

Recommend
Ignatius.L

//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;
}


posted on 2012-07-13 21:49  江财小子  阅读(255)  评论(0编辑  收藏  举报