I won’t tell you this is about number theory
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
题意:
求S
S=gcd(am−1,an−1)S=gcd(am−1,an−1)
分析:
有结论fuck!
设a>b,gcd(a,b)=1a>b,gcd(a,b)=1 则gcd(am−bm,an−bn)=agcd(m,n)−bgcd(m,n)gcd(am−bm,an−bn)=agcd(m,n)−bgcd(m,n)
特别的有 a>1,m,n>0a>1,m,n>0,则gcd(am−1,an−1)=agcd(m,n)−1gcd(am−1,an−1)=agcd(m,n)−1
code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
return b ? gcd(b,a%b) : a;
}
ll q_pow(ll a,ll b,ll mod){
ll ans = 1;
while(b){
if(b & 1)
ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
ll a,m,n,k;
scanf("%lld%lld%lld%lld",&a,&m,&n,&k);
ll g = gcd(m,n);
printf("%lld\n",(q_pow(a,g,k) - 1 + k) % k);
}
return 0;
}