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.
eg:
intput
1
1 1 1 1
output
0
题意:S = gcd(a^m-1,a^n-1)%k,求s;
需要知道一个公式:
gcd(a^(m-1),a^(n-1)) = (a^gcd(m,n))-1
推广式:
若 gcd(a,b)=1
gcd(a^m-b^m,a^n-b^n) = a^gcd(m,n)-b^gcd(m,n)
代码:
#include<cstdio>
#define ll long long
ll Gcd(ll x,ll y)
{
return y?Gcd(y,x%y):x;
}
int main()
{
ll n,m,a,k,t;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld%lld%lld",&a,&m,&n,&k);
ll s=Gcd(m,n);
ll Pow=1;
for(int i=1;i<=s;i++)
{
Pow*=a;
Pow%=k;
}
Pow%=k;
ll ans=(Pow-1+k)%k;
printf("%lld\n",ans);
}
return 0;
}