GDKOI 2024 D2T2 题解
场切此题!1111 竟然没挂分。
题意:给定非负整数
多测,
首先考虑求出
因式分解:
直接辗转相除,得原式
注意到这个东西是模
不妨把
由于我们只需要
将上列的
于是,只要我们能够求出
接下来就是求值了。
注意到,如果
如果
最后的式子是
考虑优化,显然我们只需要知道对于每个可能的
然后根号枚举
赛时代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 998244353, mod2 = 998244352, lim = 10000000, sqr = 100000;
LL qpow(LL x, LL cnt) {
LL ret = 1, bas = x, c = cnt;
while (c) {
if (c & 1) ret = ret * bas % mod;
bas = bas * bas % mod;
c >>= 1;
}
return ret;
}
int T, g[2], cur, phi[10000005], prm[2000005], tot;
bool isp[10000005];
LL m, a, b, c, d, pow1[100005], pow2[100005];
void init() {
phi[1] = 1;
for (int i = 2; i <= lim; i++) {
if (!isp[i]) {
phi[i] = i - 1;
prm[++tot] = i;
}
for (int j = 1; j <= tot && i * prm[j] <= lim; j++) {
int tmp = i * prm[j];
isp[tmp] = 1;
if (i % prm[j] == 0) {
phi[tmp] = phi[i] * prm[j];
break;
}
else phi[tmp] = phi[i] * phi[prm[j]];
}
}
pow1[0] = 1; for (int i = 1; i <= sqr; i++) pow1[i] = pow1[i - 1] * 2 % mod;
pow2[0] = 1; for (int i = 1; i <= sqr; i++) pow2[i] = pow2[i - 1] * pow1[sqr] % mod;
}
LL qmod(LL x) { return (x >= mod ? x - mod : x); }
LL qmod2(LL x) { return (x >= mod2 ? x - mod2 : x); }
LL lpow(LL cnt) { return pow1[cnt % sqr] * pow2[cnt / sqr] % mod; }
void f(LL x, LL l, LL r) {
g[cur] = -1;
if (l == 0 || r == 0) return;
g[cur] = __gcd(l, r) - 1;
}
void work() {
scanf("%lld %lld %lld %lld %lld", &m, &a, &b, &c, &d);
cur = 0; f(m, a, b); cur = 1; f(m, c, d);
LL blc = 0, res = 1;
for (int i = 1; i <= g[1]; i++) {
if (i > g[0]) blc = qmod2(blc + res);
res = res * m % mod2;
}
blc = blc * (m - 1) % mod2;
if (g[0] == -1) blc = qmod2(blc + 1);
LL all = 0;
for (int i = 1; i * i <= m; i++) {
if (m % i == 0) {
if ((m / i) & 1) all = (all + lpow(blc * i % mod2) * phi[m / i]) % mod;
if (i * i != m && (i & 1)) all = (all + lpow(blc * (m / i) % mod2) * phi[i]) % mod;
}
}
printf("%lld\n", all * qpow(m, mod - 2) % mod);
}
int main() {
freopen("calculate.in", "r", stdin);
freopen("calculate.out", "w", stdout);
init();
scanf("%d", &T);
while (T--) work();
return 0;
}