目录
0 链接
1 描述
- 已 知 N 2 − 3 N + 2 = ∑ d ∣ N f ( d ) , 求 ∑ i = 1 n f ( i ) . 已知 N^2-3N+2 = \sum_{d|N}f(d), 求\sum_{i=1}^{n}f(i). 已知N2−3N+2=∑d∣Nf(d),求∑i=1nf(i).
2 分析
2.1杜教筛
N
2
−
3
N
+
2
=
∑
d
∣
N
f
(
d
)
,
根
据
莫
比
乌
斯
反
演
得
:
N^2-3N+2 = \sum_{d|N}f(d),根据莫比乌斯反演得:
N2−3N+2=d∣N∑f(d),根据莫比乌斯反演得:
f
(
n
)
=
∑
d
∣
n
(
d
−
2
)
⋅
(
d
−
1
)
⋅
μ
(
n
d
)
f(n)=\sum_{d|n}(d-2)\cdot (d-1)\cdot \mu(\frac{n}{d})
f(n)=d∣n∑(d−2)⋅(d−1)⋅μ(dn)
( g ∗ f ) ( n ) = ∑ d ∣ n g ( d ) ⋅ f ( n d ) = ∑ d ∣ n g ( n d ) ⋅ f ( d ) (g*f)(n) = \sum_{d|n}g(d)\cdot f(\frac{n}{d}) = \sum_{d|n}g(\frac{n}{d})\cdot f(d) (g∗f)(n)=d∣n∑g(d)⋅f(dn)=d∣n∑g(dn)⋅f(d)
∑
i
=
1
n
(
g
∗
f
)
(
i
)
=
∑
i
=
1
n
∑
d
∣
i
g
(
d
)
⋅
f
(
i
d
)
\sum_{i=1}^{n}(g*f)(i)= \sum_{i=1}^{n}\sum_{d|i}g(d)\cdot f(\frac{i}{d})
i=1∑n(g∗f)(i)=i=1∑nd∣i∑g(d)⋅f(di)
=
∑
d
=
1
n
(
g
(
d
)
⋅
∑
i
d
=
1
⌊
n
d
⌋
f
(
i
d
)
)
=
∑
d
=
1
n
g
(
d
)
⋅
S
(
⌊
n
d
⌋
)
=\sum_{d=1}^{n}\left(g(d)\cdot \sum_{\frac{i}{d}=1}^{\lfloor\frac{n}{d}\rfloor}f(\frac{i}{d})\right)=\sum_{d=1}^{n}g(d)\cdot S(\lfloor\frac{n}{d}\rfloor)
=d=1∑n⎝⎛g(d)⋅di=1∑⌊dn⌋f(di)⎠⎞=d=1∑ng(d)⋅S(⌊dn⌋)
=
g
(
1
)
⋅
S
(
n
)
+
∑
d
=
2
n
g
(
d
)
⋅
S
(
⌊
n
d
⌋
)
,
式
中
S
(
k
)
=
∑
i
=
1
k
f
(
i
)
=g(1)\cdot S(n)+\sum_{d=2}^{n}g(d)\cdot S(\lfloor\frac{n}{d} \rfloor),式中S(k)=\sum_{i=1}^{k}f(i)
=g(1)⋅S(n)+d=2∑ng(d)⋅S(⌊dn⌋),式中S(k)=i=1∑kf(i)
∴
g
(
1
)
⋅
S
(
n
)
=
∑
i
=
1
n
(
g
∗
f
)
(
i
)
−
∑
d
=
2
n
g
(
d
)
⋅
S
(
⌊
n
d
⌋
)
\therefore \quad g(1)\cdot S(n)=\sum_{i=1}^{n}(g*f)(i)-\sum_{d=2}^{n}g(d)\cdot S(\lfloor\frac{n}{d} \rfloor)
∴g(1)⋅S(n)=i=1∑n(g∗f)(i)−d=2∑ng(d)⋅S(⌊dn⌋)
2.2 数学模型
∑
d
∣
N
f
(
d
)
⋅
1
=
(
f
∗
I
)
(
n
)
=
(
I
∗
f
)
(
n
)
=
∑
d
∣
N
1
⋅
f
(
n
d
)
,
式
中
I
(
n
)
=
1
\sum_{d|N}f(d)\cdot 1 = (f*I)(n)=(I*f)(n)=\sum_{d|N}1\cdot f(\frac{n}{d}) ,式中I(n)=1
d∣N∑f(d)⋅1=(f∗I)(n)=(I∗f)(n)=d∣N∑1⋅f(dn),式中I(n)=1
即
:
N
2
−
3
N
+
2
=
∑
d
∣
N
1
⋅
f
(
n
d
)
即:N^2-3N+2=\sum_{d|N}1\cdot f(\frac{n}{d})
即:N2−3N+2=d∣N∑1⋅f(dn)
S
(
N
)
=
∑
i
=
1
N
(
i
2
−
3
i
+
2
)
−
∑
d
=
2
N
1
⋅
S
(
⌊
N
d
⌋
)
S(N) =\sum_{i =1}^{N}(i^2-3i+2)-\sum_{d=2}^{N}1\cdot S(\lfloor\frac{N}{d} \rfloor)
S(N)=i=1∑N(i2−3i+2)−d=2∑N1⋅S(⌊dN⌋)
=
∑
i
=
1
n
(
i
−
2
)
(
i
−
1
)
−
∑
d
=
2
N
S
(
⌊
N
d
⌋
)
=\sum_{i =1}^{n}(i-2)(i-1)-\sum_{d=2}^{N}S(\lfloor\frac{N}{d} \rfloor)
=i=1∑n(i−2)(i−1)−d=2∑NS(⌊dN⌋)
=
n
⋅
(
n
−
1
)
⋅
(
n
−
2
)
3
−
∑
d
=
2
N
S
(
⌊
N
d
⌋
)
\quad\quad=\frac{n\cdot (n-1)\cdot (n-2)}{3}-\sum_{d=2}^{N}S(\lfloor\frac{N}{d} \rfloor)
=3n⋅(n−1)⋅(n−2)−d=2∑NS(⌊dN⌋)
3 代码
3.1 方法一【468MS】
3.1.1 说明
- 预处理函数pre()
- 根据 f ( n ) = ( n − 1 ) ( n − 2 ) − f ( 1 ) − … , n < 1 e 6 f(n)=(n-1)(n-2)-f(1)-…,n < 1e6 f(n)=(n−1)(n−2)−f(1)−…,n<1e6计算f(n),时间复杂度 O ( n ⋅ log ( n ) ) O(n\cdot \log(n)) O(n⋅log(n))
- 根据f(n)求 ∑ i = 1 n f ( i ) \sum_{i=1}^{n}f(i) ∑i=1nf(i)
- 求前缀和函数get_sum()
- 如果n>1e6,则杜教筛;否则查表
3.1.2 代码
// hdu 5608 function
#include<bits/stdc++.h>
using namespace std;
#define inv 333333336
#define mxn 1000010
#define mod 1000000007
int n, t, ans[mxn];
map<int,int> q;
void pre(){// 计算f(n)及其前缀和
for(int i = 1; i < mxn; ++i)
ans[i] = 1LL*(i-2)*(i-1)%mod;
for(int i = 1; i < mxn; ++i){// 计算f(n)
for(int j = 2*i; j < mxn; j+=i){
ans[j] -= ans[i];
if(ans[j] < 0) ans[j] += mod;
}
}
for(int i = 2; i < mxn; ++i){// f(n)的前缀和
ans[i] += ans[i-1];
if(ans[i] >= mod) ans[i] -= mod;
}
}
int get_sum(int x){
if(x < mxn) return ans[x];
if(q.count(x)) return q[x];
int res = 1LL*inv*x%mod*(x-1)%mod*(x-2)%mod;
for(int i = 2, j; i <= x; i = j+1){// 整除分块
j = x/(x/i);
res -= 1LL*(j-i+1)*get_sum(x/i)%mod;
if(res < 0) res += mod;
}
return q[x] = res;
}
int main(){
pre();
scanf("%d", &t);
while(t--){
scanf("%d", &n);
printf("%d\n", get_sum(n));
}
return 0;
}