zoukankan      html  css  js  c++  java
  • 2^x mod n = 1 HDU1395

    /*2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6738    Accepted Submission(s): 2022

    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 ZOJ Monthly, February 2003 */

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    bool
    h[10000];
    int
    main()
    {

        int
    n,k,n1,l,c;
        while
    (scanf("%d",&n)!=EOF)
        {

            memset(h,0,sizeof(h));
            k=0;l=1;c=2;
           printf("2^");
           if
    (n==1||n==2)
           {

               printf("? mod ");
               printf("%d = 1\n",n);
               continue
    ;
           }

           n1=n;
           while
    (c%n1!=1)
           {

               h[c]=1;
               c*=2;
               c=c%n1;
               l++;
               if
    (c!=1)
               {

                   if
    (h[c]==1)//经典的比较方法,与前面的c比较,值得收藏。
                   {

                       k=1;
                       break
    ;
                   }

                   else

                   h[c]=1;
               }
           }

           if
    (k==1)
           {

               printf("? mod ");
               printf("%d = 1\n",n);
               continue
    ;
           }

           else

           {


               printf("%d mod ",l);
               printf("%d = 1\n",n);
           }
        }

        return
    0;
    }//本题的为偶数时一定不到l,因为2^l=n*q+r.......r一定为偶数不会是1,所以可以把偶数单独拿出来。

  • 相关阅读:
    【剑指offer】数字在排序数组中出现的次数
    移动互联网的架构设计浅谈一
    Android开发中遇到的adb问题解决方法
    Datagrid分页、排序、删除代码
    新辰:关于个人站点安全问题的分析及对策探讨
    Android开发中,activity页面跳转后是空白
    实战——二、c#窗体(2)
    实战——一、c#窗体(1)
    c#的sealed修饰符
    c#中,类的重写中,new和override的区别
  • 原文地址:https://www.cnblogs.com/heqinghui/p/2613303.html
Copyright © 2011-2022 走看看