hdu2685gcd性质

来源:互联网 发布:php class exists 500 编辑:程序博客网 时间:2024/04/29 04:25

Problem Description
To think of a beautiful problem description is so hard for me that let’s just drop them off. :)
Given four integers a,m,n,k,and S = gcd(a^m-1,a^n-1)%k,calculate the S.

Input
The first line contain a t,then t cases followed.
Each case contain four integers a,m,n,k(1<=a,m,n,k<=10000).

Output
One line with a integer S.

Sample Input
1
1 1 1 1

Sample Output
0

gcd(a^m-b^m, a^n-b^n) = a^gcd(m,n)-b^gcd(m,n);
这里b=1;

#include <iostream>using namespace std;typedef long long LL;LL gcd(LL a,LL b){    if(!b)    return a;    return gcd(b,a%b);}LL power(LL a,LL b,LL mod){    LL ans=1;    while(b)    {        if(b&1)        {            ans=(ans*a)%mod;            b--;        }        else        {            b>>=1;            a=(a*a)%mod;        }    }    return ans;}int main(){int t; LL a,m,n,k; cin>>t; while(t--) {     cin>>a>>m>>n>>k;     cout<<(power(a,gcd(m,n),k)+k-1)%k<<endl; }    return 0;}
0 0
原创粉丝点击