当前位置:网站首页>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
边栏推荐
- USO technology was invited to share the technical framework and challenges of AI synthetic virtual characters at lvson2020 conference
- 理解补码的要点
- 启动mqbroker.cmd失败解决方法
- Jupyter Notebook 安装
- Solution of self Networking Wireless Communication intercom system in Beifeng oil and gas field
- 可视化常见问题解决方案(八)数学公式
- LATEX公式注意事项
- P1390 公约数的和(莫比乌斯反演)
- 可视化常见问题解决方案(八)共享绘图区域问题解决方案
- 两个线程交互打印奇偶数字
猜你喜欢

quill-editor图片缩放、在一个页面使用多个富文本框、quill-editor上传图片地址为服务器地址

Beifeng communication helps Zhanjiang fire brigade build PDT wireless communication system

不需要破解markdown编辑工具Typora

hql求一个范围内最大值

可视化之路(十一)matplotlib颜色详解

USO technology was invited to share the technical framework and challenges of AI synthetic virtual characters at lvson2020 conference

北峰油气田自组网无线通信对讲系统解决方案

Patrol inspection intercom communication system in power industry

可视化之路(九)Arrow类详解

Machine vision series (01) -- Overview
随机推荐
VScode
字节跳动2020秋招编程题:根据工号快速找到自己的排名
Emergency air space integrated communication system scheme of Guangxi Power Grid
H5案例开发
el-table的数据更新后,页面中数据未更新this.$forceUpdate()无效果
Emergency medical communication solution | mesh wireless ad hoc network system
城市应急管理|城市突发事故应急通信指挥调度系统
C语言的指针符号到底靠近变量类型还是变量名?
el-select 中v-model绑定值,数据回显只显示value,不显示label
技术小白的第一篇(表达自己)
SDC intelligent communication patrol management system of Nanfang investment building
Transformer的pytorch实现
presto日期函数的使用
pytorch:关于GradReverseLayer实现的一个坑
商业广场无线对讲系统解决方案
(一)OpenPAI jupyter jupyterhub jupyterlab 方案比较
菜菜的并发编程笔记 |(九)异步IO实现并发爬虫加速
PyTorch 13. Nested functions and closures (dog head)
manjaro安装与配置(vscode,微信,美化,输入法)
免费开源充电桩物联网云平台