当前位置:网站首页>Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
2022-04-23 10:33:00 【Zimba_】
summary
When the formula is optimized to ∑ d = 1 n ∑ d ∣ i s ( i ) \sum_{d=1}^{n}\sum_{d|i}s(i) ∑d=1n∑d∣is(i) In a similar form , We can o ( n l o g n ) o(nlogn) o(nlogn) do . But when n n n take 1 0 7 10^7 107 It's very dangerous when , Until today, I found such a thing ——Dirichlet The prefix and . It can be o ( n l o g l o g n ) o(nloglogn) o(nloglogn) Solve this problem within the complexity of , Fast approximation o ( n ) o(n) o(n) 了 .
This blog is used to write down the board , To learn, you can see This blog .
Dirichlet The prefix and
Formula :( Given a a a, seek b b b)
b [ d ] = ∑ i ∣ d a [ i ] b[d]=\sum_{i|d}a[i] b[d]=i∣d∑a[i]
Code :
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 Suffixes and
Formula :( Given a a a, seek b b b)
b [ d ] = ∑ d ∣ i a [ i ] b[d]=\sum_{d|i}a[i] b[d]=d∣i∑a[i]
Code :
for(int i=0;i<tot&&prime[i]<=n;i++)
{
for(int j=n/prime[i];j;j--)
{
a[j]+=a[j*prime[i]];
}
}
Push backward Dirichlet The prefix and
Formula :( Given b b b, seek a a a)
b [ d ] = ∑ i ∣ d a [ i ] b[d]=\sum_{i|d}a[i] b[d]=i∣d∑a[i]
Code :
for(int i=tot-1;i>=0;i--)
{
for(int j=n/prime[i];j;j--)
{
a[j*prime[i]]-=a[j];
}
}
Push backward Dirichlet Suffixes and
Formula :( Given b b b, seek a a a)
b [ d ] = ∑ d ∣ i a [ i ] b[d]=\sum_{d|i}a[i] b[d]=d∣i∑a[i]
Code :
for(int i=tot-1;i>=0;i--)
{
for(int j=1;j*prime[i]<=n;++j)
{
a[j]-=a[j*prime[i]];
}
}
appendix ( Matching prime sieve )
Code :
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://yzsam.com/2022/04/202204230621424645.html
边栏推荐
- Using idea to develop Spark Program
- Juc并发编程06——深入剖析队列同步器AQS源码
- Realize data value through streaming data integration (2)
- Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
- 997. Square of ordered array (array)
- 基于PyQt5实现弹出任务进度条功能示例
- How can swagger2 custom parameter annotations not be displayed
- 24. Exchange the nodes in the linked list (linked list)
- shell脚本免交互
- Yarn resource scheduler
猜你喜欢
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
SQL Server 递归查询上下级
Detailed explanation of MapReduce calculation process
【leetcode】199. Right view of binary tree
Jerry's more accurate determination of abnormal address [chapter]
IDEA——》每次启动都会Indexing或 scanning files to index
shell脚本免交互
Charles 功能介绍和使用教程
Solution architect's small bag - 5 types of architecture diagrams
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
随机推荐
一文看懂 LSTM(Long Short-Term Memory)
707、设计链表(链表)
202、快乐数
SSH uses private key to connect to server without key
【leetcode】107. Sequence traversal of binary tree II
[provincial election joint examination 2022 d2t1] card (state compression DP, FWT convolution)
【leetcode】107.二叉树的层序遍历II
Realize data value through streaming data integration (1)
What are the system events of Jerry's [chapter]
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
0704、ansible----01
链表相交(链表)
Ansible playbook syntax and format automate cloud computing
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
Example of pop-up task progress bar function based on pyqt5
Chapter II in memory architecture (im-2.2)
Can Jerry's AES 256bit [chapter]
Zhengda international explains what the Dow Jones industrial index is?
得到知识服务app原型设计比较与实践
349、两个数组的交集