Gcd Product
Cm=∑i=1mAgcd(i,m)Bgcd(k+1−i,m)∑d1∣mAd1∑d2∣mBd2∑i=1m([gcd(id1,md1)=1][d1∣i])([gcd(m+1−id2,md2)=1][d2∣m+1−i])∑d1∣mAd1∑d2∣mBd2∑k1∣md1μ(k1)∑k2∣md2μ(k2)∑i=1m([d1∣i][k1∣id1])([d2∣m+1−i][k2∣m+1−id2])T1=d1×k1,T2=d2×k2∑T1∣m∑d1∣T1Ad1μ(T1d1)∑T2∣m∑d2∣T2Bd2μ(T2d2)∑i=1m[T1∣i][T2∣m+1−i]C_m = \sum_{i = 1} ^{m} A_{\gcd(i, m)} B_{\gcd(k + 1 - i, m)}\\ \sum_{d1 \mid m} A_{d1} \sum_{d_2 \mid m}B_{d_2} \sum_{i = 1} ^{m}\left([\gcd(\frac{i}{d_1}, \frac{m}{d_1}) = 1][d_1 \mid i]\right)\left([\gcd(\frac{m + 1 - i}{d_2}, \frac{m}{d_2}) = 1][d_2 \mid m + 1 - i]\right)\\ \sum_{d_1 \mid m} A_{d_1} \sum_{d_2 \mid m} B_{d_2} \sum_{k_1 \mid \frac{m}{d_1}} \mu(k_1) \sum_{k_2 \mid \frac{m}{d_2}} \mu(k_2) \sum_{i = 1} ^{m} \left([d_1 \mid i][k_1 \mid \frac{i}{d_1}] \right)\left([d_2 \mid m + 1 - i][k_2 \mid \frac{m + 1 - i}{d_2}] \right)\\ T_1 = d_1 \times k_1, T_2 = d_2 \times k_2\\ \sum_{T_1 \mid m} \sum_{d_1 \mid T_1} A_{d_1} \mu(\frac{T_1}{d_1}) \sum_{T_2 \mid m} \sum_{d_2 \mid T_2} B_{d_2} \mu(\frac{T_2}{d_2}) \sum_{i = 1} ^{m} [T_1 \mid i][T_2 \mid m + 1 - i]\\ Cm=i=1∑mAgcd(i,m)Bgcd(k+1−i,m)d1∣m∑Ad1d2∣m∑Bd2i=1∑m([gcd(d1i,d1m)=1][d1∣i])([gcd(d2m+1−i,d2m)=1][d2∣m+1−i])d1∣m∑Ad1d2∣m∑Bd2k1∣d1m∑μ(k1)k2∣d2m∑μ(k2)i=1∑m([d1∣i][k1∣d1i])([d2∣m+1−i][k2∣d2m+1−i])T1=d1×k1,T2=d2×k2T1∣m∑d1∣T1∑Ad1μ(d1T1)T2∣m∑d2∣T2∑Bd2μ(d2T2)i=1∑m[T1∣i][T2∣m+1−i]
观察式子,不难发现∑d1∣T1Ad1μ(T1d1),∑d2∣T2Bd2μ(T2d2)\sum\limits_{d_1 \mid T_1} A_{d_1} \mu(\frac{T_1}{d_1}), \sum_{d_2 \mid T_2} B_{d_2} \mu(\frac{T_2}{d_2})d1∣T1∑Ad1μ(d1T1),∑d2∣T2Bd2μ(d2T2),二者对于给定的T1,T2T_1,T_2T1,T2都是可以确定的,跟变量mmm无关,
设f(n)=∑d∣nAdμ(nd),g(n)=∑d∣nBdμ(nd)f(n) = \sum\limits_{d \mid n} A_{d} \mu(\frac{n}{d}), g(n) = \sum_{d \mid n} B_{d} \mu(\frac{n}{d})f(n)=d∣n∑Adμ(dn),g(n)=∑d∣nBdμ(dn),得到∑T1∣mf(T1)∑T2∣mg(T2)∑i=1m[T1∣i][T2∣m+1−i]\sum\limits_{T_1 \mid m} f(T_1) \sum\limits_{T_2 \mid m} g(T_2) \sum\limits_{i = 1} ^{m}[T_1 \mid i][T_2 \mid m + 1 - i]T1∣m∑f(T1)T2∣m∑g(T2)i=1∑m[T1∣i][T2∣m+1−i],
接下来我们考虑化简∑i=1m[T1∣i][T2∣m+1−i]\sum\limits_{i = 1} ^{m} [T_1 \mid i][T_2 \mid m + 1 - i]i=1∑m[T1∣i][T2∣m+1−i]。
设i=T1k1,m+1−i=T2k2设i = T_1 k_1, m + 1 - i = T_2 k_2设i=T1k1,m+1−i=T2k2,则T1k1+T2k2=m+1T_1 k_1 + T_2 k_2 = m + 1T1k1+T2k2=m+1,可得:
同余方程T1k1≡1(modT2),T2k2≡1(modT1)T_1 k_1 \equiv 1 \pmod{T_2}, T_2 k_2 \equiv 1 \pmod{T_1}T1k1≡1(modT2),T2k2≡1(modT1)。
由T1∣i,T2∣m+1−iT_1 \mid i, T_2 \mid m + 1 - iT1∣i,T2∣m+1−i,则gcd(T1,T2)∣i,gcd(T1,T2)∣m+1−i\gcd(T_1, T_2) \mid i, \gcd(T_1, T_2) \mid m + 1 - igcd(T1,T2)∣i,gcd(T1,T2)∣m+1−i,所以gcd(T1,T2)∣m+1\gcd(T_1, T_2) \mid m + 1gcd(T1,T2)∣m+1。
由T1∣m,T2∣mT_1 \mid m, T_2 \mid mT1∣m,T2∣m,则gcd(T1,T2)∣m\gcd(T_1, T_2) \mid mgcd(T1,T2)∣m,因为gcd(m,m+1)=1gcd(m, m + 1) = 1gcd(m,m+1)=1,所以有gcd(T1,T2)=1gcd(T_1, T_2) = 1gcd(T1,T2)=1。
所以k1,k2k_1, k_2k1,k2,分别在膜T2,T1T_2, T_1T2,T1下有且只有唯一解x1,x2x_1, x_2x1,x2,有x1T1<T1T2,x2T2<T1T2x_1 T_1 < T_1 T_2, x_2 T_2 < T_1 T_2x1T1<T1T2,x2T2<T1T2,
可得x1T1+x2T2=T1T2+1x_1T_1 + x_2 T_2 = T_1T_2 + 1x1T1+x2T2=T1T2+1,要使k1T1+k2T2=m+1k_1T_1 + k_2 T_2 = m + 1k1T1+k2T2=m+1,
相当于在x1T1+x2T2=T1T2+1x_1T_1 + x_2 T_2 = T_1T_2 + 1x1T1+x2T2=T1T2+1的基础上给x1T1,x2T2x_1T_1,x_2T_2x1T1,x2T2组合分配,凑得m−T1T2m - T_1 T_2m−T1T2。
设m=KT1T2m = KT_1T_2m=KT1T2,所以解的个数就是K=mT1T2K = \frac{m}{T_1T_2}K=T1T2m,则有:
Cm=∑T1∣mf(T1)∑T2∣mg(T2)mT1T2[gcd(T1,T2)=1]T=T1T2∑T∣mmT∑T1∣Tf(T1)g(TT1)[gcd(T1,TT2)=1]C_m = \sum_{T_1 \mid m} f(T_1) \sum_{T_2 \mid m} g(T_2) \frac{m}{T_1T_2}[\gcd(T_1, T_2) = 1]\\ T = T_1 T_2\\ \sum_{T \mid m} \frac{m}{T} \sum_{T_1 \mid T} f(T_1) g(\frac{T}{T_1})[gcd(T_1, \frac{T}{T_2}) = 1]\\ Cm=T1∣m∑f(T1)T2∣m∑g(T2)T1T2m[gcd(T1,T2)=1]T=T1T2T∣m∑TmT1∣T∑f(T1)g(T1T)[gcd(T1,T2T)=1]
先做一次迪利克雷卷积得到f,gf, gf,g,再做一次互质迪利克雷卷积得到∑T1∣Tf(T1)g(TT1)[gcd(T1,TT2)=1]\sum\limits_{T_1 \mid T} f(T_1) g(\frac{T}{T_1})[gcd(T_1, \frac{T}{T_2}) = 1]T1∣T∑f(T1)g(T1T)[gcd(T1,T2T)=1],最后迪利克雷卷积得到答案。
#include <bits/stdc++.h>using namespace std;const int N = 5e5 + 10, mod = 998244353;int prime[N], mu[N], phi[N], A[N], B[N], f[N], g[N], h[N], ans[N], n, cnt;bool st[N];void init() {mu[1] = phi[1] = 1;for (int i = 2; i < N; i++) {if (!st[i]) {prime[++cnt] = i;mu[i] = mod - 1;phi[i] = i - 1;}for (int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if (i % prime[j] == 0) {phi[i * prime[j]] = phi[i] * prime[j];break;}phi[i * prime[j]] = phi[i] * (prime[j] - 1);mu[i * prime[j]] = mod - mu[i];}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &A[i]);}for (int i = 1; i <= n; i++) {scanf("%d", &B[i]);}for (int i = 1; i <= n; i++) {for (int j = i; j <= n; j += i) {f[j] = (f[j] + 1ll * A[i] * mu[j / i] % mod) % mod;g[j] = (g[j] + 1ll * B[i] * mu[j / i] % mod) % mod;}}for (int i = 1; i <= n; i++) {for (int j = i; j <= n; j += i) {if (1ll * phi[i] * phi[j / i] == phi[j]) {h[j] = (h[j] + 1ll * f[i] * g[j / i] % mod) % mod;}}}for (int i = 1; i <= n; i++) {for (int j = i; j <= n; j += i) {ans[j] = (ans[j] + 1ll * (j / i) * h[i] % mod) % mod;}}for (int i = 1; i <= n; i++) {ans[i] ^= ans[i - 1];}printf("%d\n", ans[n]);return 0;
}