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


  • 相关阅读:
    开源界的 5 大开源许可协议
    如何选择开源许可证?
    Ubuntu下Qt编译报错“cannot find -lGL”的解决方案
    How to Cracked Sublime Text 3 Build 3065 in Ubuntu (Linux)
    一个C语言宏展开问题
    C语言预处理运算符
    Linux线程编程之信号处理
    Linux终端多用户通信实用命令
    守护进程接收终端输入的一种变通性方法(二)
    通过printf设置Linux终端输出的颜色和显示方式
  • 原文地址:https://www.cnblogs.com/372465774y/p/2590807.html
Copyright © 2011-2022 走看看