HDU 5608 function(杜教筛)

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). N23N+2=dNf(d)i=1nf(i).

2 分析

2.1杜教筛

  • 2.1.1 莫比乌斯反演

N 2 − 3 N + 2 = ∑ d ∣ N f ( d ) , 根 据 莫 比 乌 斯 反 演 得 : N^2-3N+2 = \sum_{d|N}f(d),根据莫比乌斯反演得: N23N+2=dNf(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)=dn(d2)(d1)μ(dn)

  • 2.1.2 迪立克利卷积 g ∗ f g*f gf

( 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) (gf)(n)=dng(d)f(dn)=dng(dn)f(d)

  • 2.1.3 杜教筛

∑ 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=1n(gf)(i)=i=1ndig(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=1ng(d)di=1dnf(di)=d=1ng(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=2ng(d)S(dn)S(k)=i=1kf(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=1n(gf)(i)d=2ng(d)S(dn)

2.2 数学模型

  • 2.2.1 已知条件

∑ 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 dNf(d)1=(fI)(n)=(If)(n)=dN1f(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}) N23N+2=dN1f(dn)

  • 2.2.2 结论

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=1N(i23i+2)d=2N1S(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=1n(i2)(i1)d=2NS(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(n1)(n2)d=2NS(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)=(n1)(n2)f(1)n<1e6计算f(n),时间复杂度 O ( n ⋅ log ⁡ ( n ) ) O(n\cdot \log(n)) O(nlog(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;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jpphy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值