GDKOI 2024 D2T2 题解

· · 题解

场切此题!1111 竟然没挂分。

题意:给定非负整数 m,a,b,c,d,求 [\gcd(m^{a}-1,m^{b}-1) + 2,\gcd(m^{c}-1,m^{d}-1)+1] 中的整数有多少个子集的和为 m 的倍数。约定 \gcd(0,n)=-1。

多测,T \le 10^{4},m \le 10^{7},a,b,c,d \le 10^{3}。

首先考虑求出 \gcd(m^{a}-1,m^{b}-1)。

因式分解:m^{a}-1=(m-1)(m^{a-1}+m^{a-2}+\cdots+m+1)。于是可以约掉一个 m-1,问题变成求 \gcd(m^{a-1}+m^{a-2}+\cdots+m+1,m^{b-1}+m^{b-2}+\cdots+m+1)。

直接辗转相除,得原式 =(m-1)(m^{\gcd(a,b)-1}+m^{\gcd(a,b)-2}+\cdots+m+1)。这部分可以直接 O(a) 处理出来(默认 a,b,c,d 同阶)。

注意到这个东西是模 m 余 -1 的,原因显然。所以接下来我们进入到第二部分:求 [L,R] 间的整数有多少个子集的和为 m 的倍数,且保证 R-L+1 为 m 的倍数。

不妨把 [L,R] 平移至 [1,R-L+1]。然后考虑子集和为 i 的方案数 a_{i} 的生成函数 f(x)=(x+1)(x^2+1)(x^{3}+1)\cdots(x^{R-L+1}+1)=x^{0}a_{0}+x^{1}a_{1}+x^{2}a_{2}+\cdots。

由于我们只需要 i 为 m 的倍数的项,根据经典套路可以分别带入 m 个 m 次单位根后将这些式子的值全部相加来消掉。即:

\begin{aligned} f(\omega_{m}^{0})&=\omega_m^0a_0+\omega_m^0a_1+\cdots+\omega_m^0a_{m-1}+\cdots\\ f(\omega_{m}^{1})&=\omega_m^0a_0+\omega_m^1a_1+\cdots+\omega_m^{m-1}a_{m-1}+\cdots\\ f(\omega_{m}^{2})&=\omega_m^0a_0+\omega_m^2a_1+\cdots+\omega_m^{m-2}a_{m-1}+\cdots\\ &\cdots\\ f(\omega_{m}^{m-1})&=\omega_m^0a_0+\omega_m^{m-1}+\cdots+\omega_m^1a_{m-1}+\cdots \end{aligned}

将上列的 m 个式子相加,得到 a_{p} 的系数为 \displaystyle \sum_{k=0}^{m-1}\omega_m^{kp}。当 p\neq 0 时,该式恒为 0(易证)。当 p=0 时,该式的值为 m。

于是,只要我们能够求出 f(x) 在所有 m 次单位根处的取值,加起来除以 m 就是答案。

接下来就是求值了。

注意到,如果 \gcd(p,m)=1,那么对于 k\in[0,m-1],\{kp\} 构成模 m 的完系,f(\omega_m^p)=(\omega_m^0+1)(\omega_m^1+1)\cdots(\omega_m^{m-1}+1)\cdots。注意到 x^m-1=(x-\omega_m^0)(x-\omega_m^1)\cdots(x-\omega_m^{m-1}),代入 x=-1 得到 (-1)^m-1=(-1-\omega_m^0)(-1-\omega_m^1)\cdots(-1-\omega_m^{m-1})。故当 m 为偶数时,(\omega_m^0+1)(\omega_m^1+1)\cdots(\omega_m^{m-1}+1)=0;m 为奇数时,(\omega_m^0+1)(\omega_m^1+1)\cdots(\omega_m^{m-1}+1)=2。则 f(\omega_m^p)=[m\bmod2=1]2^{\frac{R-L+1}{m}}。

如果 \gcd(p,m)\neq 1,我们也可以使用类似的思路。设 \gcd(p,m)=g,那么对于 k\in[0,\frac{m}{g}-1],\{\frac{kp}{g}\} 构成模 \frac{m}{g} 的完系。然后也是类似的推导。

最后的式子是 \displaystyle \sum_{p=0}^{m-1}f(\omega_m^p)=\sum_{p=0}^{m-1}[\frac{m}{\gcd(p,m)}\bmod 2=1]2^{\frac{R-L+1}{m}\cdot \gcd(p,m)}。直接拿这个式子算是 \mathcal{O}(Tm\log m) 的,可以获得 85pts 的高分。

考虑优化,显然我们只需要知道对于每个可能的 g,\gcd(p,m)=g 的 p 有多少个。\gcd(\frac{p}{g},\frac{m}{g})=1,所以有 \varphi(\frac{m}{g}) 个。

然后根号枚举 g 即可。总时间复杂度为 \mathcal{O}(m+T(a+\sqrt{m}))。

赛时代码:

#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;
}