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

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2685

思路:gcd(A^m-B^m,A^n-B^n)= A^gcd(m,n) - B^gcd(m,n) 这里特殊情况B=1 

 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
long long gcd(long long a,long long b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
long long Pow(long long a,long long b,long long mod)
{
    long long ans=1;
    while(b)
    {
        if(b&1)
        {
            b--;
            ans=(ans*a)%mod;
        }
        else
        {
            b/=2;
            a=(a*a)%mod;
        }
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long a,m,n,k;
        scanf("%I64d%I64d%I64d%I64d",&a,&m,&n,&k);
        printf("%I64d\n",(Pow(a,gcd(m,n),k)+k-1)%k);
    }
    return 0;
}
View Code

 

posted @ 2013-07-17 17:18  over_flow  阅读(248)  评论(0编辑  收藏  举报