题解:P10580 [蓝桥杯 2024 国 A] gcd 与 lcm
yezicong1104 · · 题解
思路
由题得:
设
令
首先,对于
为了使
综上所述,对于每个
时间复杂度:
AC Code
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
const int mod = 998244353;
LL f(LL a, int b) { //快速幂
LL res = 1;
while (b) {
if (b & 1) res = (res * a) % mod;
b >>= 1, a = a * a % mod;
} return res;
}
int main() {
int Q;
scanf("%d", &Q);
vector<int> v; //存储质因数的指数
while (Q--) {
int x, y, n, p = 2;
scanf("%d%d%d", &x, &y, &n);
int t = y / x; LL pro = 1;
v.clear(); //初始化
while (p * p <= t) {
if (t % p == 0) {v.push_back(0); while (t % p == 0) t /= p, v.back()++; }
else p++;
} if (t != 1) v.push_back(1); //剩下的是一个质数
for (int i : v) pro = pro * (f(i + 1, n) - f(i, n) * 2 + f(i - 1, n)) % mod; //计算答案
printf("%lld\n", (pro + mod) % mod); //加上 mod 避免负数
}
return 0;
}