zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    XML Serializable Generic Dictionary
    MSBuild Community Tasks Project
    Firebird 数据库资源
    Atlas Samples & Suse Linux 10.1
    IBATISNETNET 1.3 开发指南系列文章
    线程安全的Generic Dictionary
    商务智能:SQL2005给我们的机会
    准备写一个Ibatisnet开发指南
    取到当前正在执行的script元素
    IE10 CSS hack
  • 原文地址:https://www.cnblogs.com/372465774y/p/2590807.html
Copyright © 2011-2022 走看看