HDU 2685 I won't tell you this is about number theory

时间:2023-03-09 21:16:07
HDU 2685 I won't tell you this is about number theory

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2685

题意:求gcd(a^m - 1, a^n - 1) mod k

思路:gcd(a^m - 1, a^n - 1) = a^gcd(m, n) - 1

code:

 #include <stdio.h>

 int gcd(int a, int b)
{
return !b ? a : gcd(b, a%b);
} int mod_pow(int a, int x, int mod)
{
int tmp = a;
int ret = ;
while(x) {
if (x & ) {
ret = ret * tmp % mod;
}
tmp = tmp * tmp % mod;
x >>= ;
} return ret;
} int main()
{
int t, a, m, n, k;
scanf("%d", &t);
while (t--) {
scanf("%d %d %d %d", &a, &m, &n, &k);
int d = gcd(m, n);
int ans = mod_pow(a, d, k);
printf("%d\n", (ans - + k) % k);
} return ;
}