Topic Links:
I won ' t tell you that this is about number theory
problem DescriptionTo think of a beautiful problem description was so hard for me the 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.
InputThe first line contain a t,then t cases followed.
Each case contain four integers a,m,n,k (1<=a,m,n,k<=10000).
OutputOne line with a integer S.
Sample Input11 1 1 1
Sample Output0
Test Instructions: The value of GCD (a^m-1,a^n-1)%k is obtained;
Ideas: There is theorem gcd (a^m-b^m,a^n-b^n) =a^gcd (n,m)-B^GCD (n,m); so gcd (a^m-1,a^n-1)%k = = (A^GCD (n,m)-1)%k;
AC Code:
#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;//const LL mod=1e9+7;Const intn=1e5+6;intgcdintXinty) { if(Y = =0)returnx; returnGCD (y,x%y);}intFastpow (intXintYintz) { ints =1,Base=x; while(y) {if(y&1) {s*=Base; S%=Z; } Base*=Base; Base%=Z; Y= (y>>1); } returns;}intFunintXintYintZintMoD) { inttemp =gcd (y, z); return(Fastpow (X,temp,mod)-1+MOD)%MoD;}intMain () {intT; scanf ("%d",&t); inta,n,m,k; while(T--) {scanf ("%d%d%d%d",&a,&n,&m,&k); printf ("%d\n", Fun (A, n, M, k)); } return 0;}
Hdu-2685i won ' t tell you this is about number theory (number theory)