hdu 2685 I won't tell you this is about number theory

#include<stdio.h>
#include<string.h>
#define LL __int64

LL mult_mod(LL a,LL b,LL c)
{
    a%=c;
    b%=c;
    LL ret=0;
    while(b)
    {
        if(b&1){ret+=a;ret%=c;}
        a<<=1;
        if(a>=c)a%=c;
        b>>=1;
    }
    return ret;
}

LL PowerMod(LL a, LL b, LL c)
{
    
    LL ans = 1;
    LL k = a % c;
    while(b>0)    //(k*k % c)2^b %c
    {
        if(b&1)
            ans = mult_mod(ans,k,c);
        b = b>>1;
        k = mult_mod(k,k,c);
    }
    return ans;
}

LL gcd(LL a,LL b)
{    
    if(a==0){return b;}
    return gcd(b%a,a);
}
int main()
{
    int T;
    scanf("%d",&T);
    LL a,m,n,k,G;
    while(T--)
    {
        scanf("%I64d%I64d%I64d%I64d",&a,&m,&n,&k);
        if(m>n)
        {
            G = gcd(n,m);
        }
        else
        {
            G = gcd(m,n);
        }
        printf("%I64d ",(PowerMod(a,G,k)+k-1%k)%k);
    }
}

/*
    gcd(a^m - 1,a^n - 1) = a^gcd(m,n) - 1


hint:
    计算减法的时候 为了保证结果是正数。
    a-b % k   ->  (a%k + k - b%k)%k

 
*/

原文地址:https://www.cnblogs.com/Milkor/p/4561363.html