版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
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;
这里用一个快速幂 取模的板子就ok啦
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string.h>
#include<iterator>
#include<vector>
#include<stdlib.h>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<sstream>
#define lowbit(x) (x&(-x))
typedef long long ll;
using namespace std;
int MOD;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
long long pow_m(long long a,long long n)
{
long long ret = 1;
long long tmp = a%MOD;
while(n)
{
if(n&1)ret = (ret*tmp)%MOD;
tmp = tmp*tmp%MOD;
n >>= 1;
}
return ret;
}
int main()
{
int a,b,c;
while(t--)
{
ll a,b,n,k;
scanf("%lld%lld%lld%lld",&a,&m,&n,&k);
MOD = k;
ll p = gcd(m,n);
printf("%lld\n",(MOD+pow_m(a,p)-1)%MOD);
}
return 0;
}