当前位置:网站首页>Dirichlet 前缀和(数论优化式子复杂度利器)
Dirichlet 前缀和(数论优化式子复杂度利器)
2022-04-23 06:21:00 【Zimba_】
概述
当式子优化到 ∑ d = 1 n ∑ d ∣ i s ( i ) \sum_{d=1}^{n}\sum_{d|i}s(i) ∑d=1n∑d∣is(i)类似形式时,我们可以 o ( n l o g n ) o(nlogn) o(nlogn)做。但是当 n n n取 1 0 7 10^7 107时就十分危险了,直到今天发现了这么个东西——Dirichlet 前缀和。它可以在 o ( n l o g l o g n ) o(nloglogn) o(nloglogn)的复杂度内解决这个问题,快近似 o ( n ) o(n) o(n)了。
这篇博客用来记板子,要学可以看这篇博客。
Dirichlet 前缀和
式子:(给定 a a a,求 b b b)
b [ d ] = ∑ i ∣ d a [ i ] b[d]=\sum_{i|d}a[i] b[d]=i∣d∑a[i]
代码:
for(int i=0;i<tot&&prime[i]<=n;i++)
{
for(int j=1;j*prime[i]<=n;j++)
{
a[j*prime[i]]+=a[j];
}
}
Dirichlet 后缀和
式子:(给定 a a a,求 b b b)
b [ d ] = ∑ d ∣ i a [ i ] b[d]=\sum_{d|i}a[i] b[d]=d∣i∑a[i]
代码:
for(int i=0;i<tot&&prime[i]<=n;i++)
{
for(int j=n/prime[i];j;j--)
{
a[j]+=a[j*prime[i]];
}
}
倒推Dirichlet 前缀和
式子:(给定 b b b,求 a a a)
b [ d ] = ∑ i ∣ d a [ i ] b[d]=\sum_{i|d}a[i] b[d]=i∣d∑a[i]
代码:
for(int i=tot-1;i>=0;i--)
{
for(int j=n/prime[i];j;j--)
{
a[j*prime[i]]-=a[j];
}
}
倒推Dirichlet 后缀和
式子:(给定 b b b,求 a a a)
b [ d ] = ∑ d ∣ i a [ i ] b[d]=\sum_{d|i}a[i] b[d]=d∣i∑a[i]
代码:
for(int i=tot-1;i>=0;i--)
{
for(int j=1;j*prime[i]<=n;++j)
{
a[j]-=a[j*prime[i]];
}
}
附录(配套素数筛)
代码:
int notPrime[MAXN],prime[MAXN],tot;
void getPrime(int n)
{
notPrime[1]=1;
for(int i=2;i<=n;i++)
{
if(!notPrime[i])prime[tot++]=i;
for(int j=0;j<tot&&i*prime[j]<=n;j++)
{
notPrime[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43785386/article/details/112845979
边栏推荐
- Metro wireless intercom system
- 两个线程交互打印奇偶数字
- Pycharm
- 快速下载vscode的方法
- LATEX使用
- Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference
- [COCI]Lampice (二分+树分治+字符串哈希)
- 在项目中的定时作用
- VIM使用
- 可视化常见绘图(四)柱状图
猜你喜欢
随机推荐
字节跳动2020秋招编程题:根据工号快速找到自己的排名
记录阿里云服务器挖矿程序处理
The difference between null and undefined
北峰油气田自组网无线通信对讲系统解决方案
地铁无线对讲系统
HuggingFace
Us photo cloud editing helps BiliBili upgrade its experience
Intelligent communication solution of Hainan Phoenix Airport
van-uploader上传图片实现过程、使用原生input实现上传图片
自定义钉钉机器人进行报警
How to improve the service efficiency of the hotel without blind spots and long endurance | public and Private Integrated walkie talkie?
学习笔记6-几种深度学习卷积神经网络的总结
Jupyter Notebook 安装
可视化常见绘图(五)散点图
Solution of self Networking Wireless Communication intercom system in Beifeng oil and gas field
PC端一次启动多个微信
基于可视化结构的身份证号码校验系统-树莓派实现
PyTorch 22. Pytorch common code snippet collection
青龙面板拉库命令更新【2022/4/20】收藏不走丢
The people of Beifeng have been taking action









