Gcd Product
C
m
=
∑
i
=
1
m
A
gcd
(
i
,
m
)
B
gcd
(
k
+
1
−
i
,
m
)
∑
d
1
∣
m
A
d
1
∑
d
2
∣
m
B
d
2
∑
i
=
1
m
(
[
gcd
(
i
d
1
,
m
d
1
)
=
1
]
[
d
1
∣
i
]
)
(
[
gcd
(
m
+
1
−
i
d
2
,
m
d
2
)
=
1
]
[
d
2
∣
m
+
1
−
i
]
)
∑
d
1
∣
m
A
d
1
∑
d
2
∣
m
B
d
2
∑
k
1
∣
m
d
1
μ
(
k
1
)
∑
k
2
∣
m
d
2
μ
(
k
2
)
∑
i
=
1
m
(
[
d
1
∣
i
]
[
k
1
∣
i
d
1
]
)
(
[
d
2
∣
m
+
1
−
i
]
[
k
2
∣
m
+
1
−
i
d
2
]
)
T
1
=
d
1
×
k
1
,
T
2
=
d
2
×
k
2
∑
T
1
∣
m
∑
d
1
∣
T
1
A
d
1
μ
(
T
1
d
1
)
∑
T
2
∣
m
∑
d
2
∣
T
2
B
d
2
μ
(
T
2
d
2
)
∑
i
=
1
m
[
T
1
∣
i
]
[
T
2
∣
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]
观察式子,不难发现
∑
d
1
∣
T
1
A
d
1
μ
(
T
1
d
1
)
,
∑
d
2
∣
T
2
B
d
2
μ
(
T
2
d
2
)
\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),二者对于给定的
T
1
,
T
2
T_1,T_2
T1,T2都是可以确定的,跟变量
m
m
m无关,
设 f ( n ) = ∑ d ∣ n A d μ ( n d ) , g ( n ) = ∑ d ∣ n B d μ ( n d ) 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),得到 ∑ T 1 ∣ m f ( T 1 ) ∑ T 2 ∣ m g ( T 2 ) ∑ i = 1 m [ T 1 ∣ i ] [ T 2 ∣ 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 = 1 m [ T 1 ∣ i ] [ T 2 ∣ 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 = T 1 k 1 , m + 1 − i = T 2 k 2 设i = T_1 k_1, m + 1 - i = T_2 k_2 设i=T1k1,m+1−i=T2k2,则 T 1 k 1 + T 2 k 2 = m + 1 T_1 k_1 + T_2 k_2 = m + 1 T1k1+T2k2=m+1,可得:
同余方程 T 1 k 1 ≡ 1 ( m o d T 2 ) , T 2 k 2 ≡ 1 ( m o d T 1 ) T_1 k_1 \equiv 1 \pmod{T_2}, T_2 k_2 \equiv 1 \pmod{T_1} T1k1≡1(modT2),T2k2≡1(modT1)。
由 T 1 ∣ i , T 2 ∣ m + 1 − i T_1 \mid i, T_2 \mid m + 1 - i T1∣i,T2∣m+1−i,则 gcd ( T 1 , T 2 ) ∣ i , gcd ( T 1 , T 2 ) ∣ m + 1 − i \gcd(T_1, T_2) \mid i, \gcd(T_1, T_2) \mid m + 1 - i gcd(T1,T2)∣i,gcd(T1,T2)∣m+1−i,所以 gcd ( T 1 , T 2 ) ∣ m + 1 \gcd(T_1, T_2) \mid m + 1 gcd(T1,T2)∣m+1。
由 T 1 ∣ m , T 2 ∣ m T_1 \mid m, T_2 \mid m T1∣m,T2∣m,则 gcd ( T 1 , T 2 ) ∣ m \gcd(T_1, T_2) \mid m gcd(T1,T2)∣m,因为 g c d ( m , m + 1 ) = 1 gcd(m, m + 1) = 1 gcd(m,m+1)=1,所以有 g c d ( T 1 , T 2 ) = 1 gcd(T_1, T_2) = 1 gcd(T1,T2)=1。
所以 k 1 , k 2 k_1, k_2 k1,k2,分别在膜 T 2 , T 1 T_2, T_1 T2,T1下有且只有唯一解 x 1 , x 2 x_1, x_2 x1,x2,有 x 1 T 1 < T 1 T 2 , x 2 T 2 < T 1 T 2 x_1 T_1 < T_1 T_2, x_2 T_2 < T_1 T_2 x1T1<T1T2,x2T2<T1T2,
可得 x 1 T 1 + x 2 T 2 = T 1 T 2 + 1 x_1T_1 + x_2 T_2 = T_1T_2 + 1 x1T1+x2T2=T1T2+1,要使 k 1 T 1 + k 2 T 2 = m + 1 k_1T_1 + k_2 T_2 = m + 1 k1T1+k2T2=m+1,
相当于在 x 1 T 1 + x 2 T 2 = T 1 T 2 + 1 x_1T_1 + x_2 T_2 = T_1T_2 + 1 x1T1+x2T2=T1T2+1的基础上给 x 1 T 1 , x 2 T 2 x_1T_1,x_2T_2 x1T1,x2T2组合分配,凑得 m − T 1 T 2 m - T_1 T_2 m−T1T2。
设
m
=
K
T
1
T
2
m = KT_1T_2
m=KT1T2,所以解的个数就是
K
=
m
T
1
T
2
K = \frac{m}{T_1T_2}
K=T1T2m,则有:
C
m
=
∑
T
1
∣
m
f
(
T
1
)
∑
T
2
∣
m
g
(
T
2
)
m
T
1
T
2
[
gcd
(
T
1
,
T
2
)
=
1
]
T
=
T
1
T
2
∑
T
∣
m
m
T
∑
T
1
∣
T
f
(
T
1
)
g
(
T
T
1
)
[
g
c
d
(
T
1
,
T
T
2
)
=
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
,
g
f, g
f,g,再做一次互质迪利克雷卷积得到
∑
T
1
∣
T
f
(
T
1
)
g
(
T
T
1
)
[
g
c
d
(
T
1
,
T
T
2
)
=
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;
}