hdu-2685 I won't tell you this is about number theory(数论)
题目链接:
I won't tell you this is about number theory
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.
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).
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-1,a^n-1)%k的值;
思路:
有定理gcd(a^m-b^m,a^n-b^n)=a^gcd(n,m)-b^gcd(n,m);
所以 gcd(a^m-1,a^n-1)%k == (a^gcd(n,m)-1)%k;
AC代码:
#include <bits/stdc++.h> using namespace std; typedef long long LL; //const LL mod=1e9+7; const int N=1e5+6; int gcd(int x,int y) { if( y == 0 )return x; return gcd( y,x%y ); } int fastpow(int x,int y,int z) { int s = 1, base = x; while(y) { if(y&1) { s *= base; s %= z; } base *= base; base %= z; y = (y>>1); } return s; } int fun( int x,int y,int z,int mod) { int temp = gcd( y, z); return (fastpow(x,temp,mod)-1+mod)%mod; } int main() { int t; scanf( "%d",&t ); int a,n,m,k; while( t -- ) { scanf( "%d%d%d%d",&a,&n,&m,&k ); printf( "%d\n",fun( a, n, m, k)); } return 0; }