HDOJ 1395 2^x mod n = 1(数论)

//http://acm.hdu.edu.cn/showproblem.php?pid=1395
//同样,快速幂取余不用解释,注意点输出格式就行了
//注意全部使用位运算
#include<iostream>

using  namespace std;


__int64 Montgomery(int a, int b, int r)
{
    __int64 ans=1, buff=a;
    while(b)
    {
        if(b&1)    ans = ans*buff%r;
        buff = buff*buff%r;
        b>>=1;
    }
    return ans;
}
int main()
{
    int n;
    int flag,i;
    while(scanf("%d",&n)!=EOF)
    {
        if(!(n&1)||n<=1)
            printf("2^? mod %d = 1
",n);
        else
            for(i=1;;i++)
            {
                if(Montgomery(2,i,n) == 1)
                {
                    printf("2^%d mod %d = 1
",i,n);
                    break;
                }
            }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Lee-geeker/p/3374966.html