题面:bzoj4916
题意:求 ∑ i = 1 n μ ( i 2 ) \sum _{i=1} ^n \mu (i^2) ∑i=1nμ(i2)和 ∑ i = 1 n φ ( i 2 ) \sum _{i=1} ^n \varphi (i ^2) ∑i=1nφ(i2),膜 1 0 9 + 7 10^9+7 109+7。 n ≤ 1 0 9 n \le 10^9 n≤109
题解:
第一个式子 ∑ i = 1 n μ ( i 2 ) \sum _{i=1} ^n \mu (i^2) ∑i=1nμ(i2),明显只有当 i = 1 i=1 i=1时, μ ( i 2 ) = 1 \mu (i ^2) = 1 μ(i2)=1; i ̸ = 1 i \not= 1 i̸=1, μ ( i 2 ) = 0 \mu (i ^2) = 0 μ(i2)=0
输出1即可。
第二个式子考虑线性筛 φ ( i ) \varphi (i) φ(i)的过程: φ ( i 2 ) = i ⋅ φ ( i ) \varphi (i^2) = i \cdot \varphi (i) φ(i2)=i⋅φ(i)
即求 ∑ i = 1 n i ⋅ φ ( i ) \sum _{i=1} ^n i \cdot \varphi (i) ∑i=1ni⋅φ(i)
设 f ( i ) = i ⋅ φ ( i ) f(i) = i \cdot \varphi (i) f(i)=i⋅φ(i), S ( n ) = ∑ i = 1 n f ( i ) S (n) = \sum _{i=1} ^n f(i) S(n)=∑i=1nf(i)
g ( 1 ) S ( n ) = ∑ i = 1 n ( f ∗ g ) ( i ) − ∑ i = 2 n g ( i ) S ( n i ) = ∑ i = 1 n ∑ d ∣ i f ( d ) ⋅ g ( i d ) − ∑ i = 2 n g ( i ) S ( n i ) = ∑ i = 1 n ∑ d ∣ i d ⋅ φ ( d ) ⋅ g ( i d ) − ∑ i = 2 n g ( i ) S ( n i ) \begin {aligned} g(1)S(n) &= \sum _{i=1} ^n (f * g) (i) - \sum _{i = 2} ^n g(i) S(\frac n i) \\ &= \sum _{i=1} ^n \sum _{d \mid i} f (d) \cdot g(\frac i d) - \sum _{i=2} ^n g(i) S(\frac n i) \\ &= \sum _{i=1} ^n \sum _{d \mid i} d \cdot \varphi (d) \cdot g (\frac i d) - \sum _{i=2} ^n g(i) S(\frac n i) \end {aligned} g(1)S(n)=i=1∑n(f∗g)(i)−i=2∑ng(i)S(in)=i=1∑nd∣i∑f(d)⋅g(di)−i=2∑ng(i)S(in)=i=1∑nd∣i∑d⋅φ(d)⋅g(di)−i=2∑ng(i)S(in)
令 g ( i ) = i g(i) = i g(i)=i
S ( n ) = ∑ i = 1 n ∑ d ∣ i d ⋅ φ ( d ) ⋅ i d − ∑ i = 2 n i S ( n i ) = ∑ i = 1 n i ∑ d ∣ i φ ( d ) − ∑ i = 2 n i ⋅ S ( n i ) = ∑ i = 1 n i 2 − ∑ i = 2 n i ⋅ S ( n i ) \begin {aligned} S(n) &= \sum _{i=1} ^n \sum _{d \mid i} d \cdot \varphi (d) \cdot \frac i d - \sum _{i=2} ^n i S(\frac n i) \\ &= \sum _{i=1} ^n i \sum _{d \mid i} \varphi (d) - \sum _{i=2} ^n i \cdot S(\frac n i) \\ &= \sum _{i=1} ^n i^2 - \sum _{i=2} ^n i \cdot S(\frac n i) \end {aligned} S(n)=i=1∑nd∣i∑d⋅φ(d)⋅di−i=2∑niS(in)=i=1∑nid∣i∑φ(d)−i=2∑ni⋅S(in)=i=1∑ni2−i=2∑ni⋅S(in)
记忆化计算即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
const int maxn = 1e6 + 10, mod = 1e9 + 7;
int prime[maxn], tot ;
ll phi[maxn], inv6 ;
bool vis[maxn] ;
map<int, ll> val ;
void init () {
phi[1] = 1 ;
for (int i = 2; i < maxn; i ++) {
if (!vis[i]) prime[++ tot] = i, phi[i] = i - 1 ;
for (int j = 1; j <= tot && i * prime[j] < maxn; j ++) {
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j] ;
break ;
}
phi[i * prime[j]] = phi[i] * phi[prime[j]] ;
}
}
for (int i = 1; i < maxn; i ++) phi[i] = (phi[i - 1] + 1ll * i * phi[i] % mod) % mod ;
}
ll power (ll x, int y) {
ll res = 1 ;
while (y) {
if (y & 1) res = res * x % mod ;
x = x * x % mod; y >>= 1 ;
}
return res ;
}
ll cal (int n) {
if (n < maxn) return phi[n] ;
if (val[n]) return val[n] ;
ll res = 1ll * n * (n + 1) % mod * (n + n + 1) % mod * inv6 % mod ;
for (int i = 2, j; i <= n; i = j + 1) {
j = n / (n / i) ;
res = (res - 1ll * (i + j) * (j - i + 1) / 2 % mod * cal (n / i) % mod) % mod ;
}
return val[n] = (res + mod) % mod ;
}
int main() {
init () ;
inv6 = power (6, mod - 2) ;
int n ;
scanf("%d", &n) ;
printf("1 %lld\n", cal (n)) ;
}