Hdu 1395 2^x mod n = 1

由于数据比较弱,所以没用欧拉函数直接枚举也可以过。

思路:n为1或者n为2的倍数时一定不存在,为奇数时一定存在。    (ab) % n = (a%n * b%n) % n;

CODE:

#include <stdio.h>
#include <stdlib.h>
#include < string.h>
using  namespace std;

int main()
{
     int n;
     while(~scanf( " %d ", &n))
    {
         int i;
         if(n ==  1 || n% 2 ==  0)
        {
            printf( " 2^? mod %d = 1\n ", n);
             continue;
        }
         long ans =  2;
         for(i =  01; i++)
        {
             if(ans%n ==  1break;
            ans = (ans%n *  2%n)%n;
        }
        printf( " 2^%d mod %d = 1\n ", i+ 1, n);
    }
     return  0;

} 

你可能感兴趣的:(HDU)