51nod 1244 莫比乌斯函数之和(杜教筛)
【题目链接】
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244
【题目大意】
计算莫比乌斯函数的区段和
【题解】
利用杜教筛:
求F(n)=∑(f(i))
存在g=f*I,定义G(n)=∑(g(i))
就可以得到F(n)=G(n)-∑(F(n/i))
加一些预处理我们可以做到O(n^(2/3))求解F(n)
我们知道积性函数∑(miu(d))=0(d|n),又有∑(miu(d))=1(n=1),
所以∑∑(miu(d))=1(d|i){i=1}^{n},
因此我们得到F(n)=1-∑F(n/d){d=2}^{n}
同时用Hash记忆化miu函数的前缀和
【代码】
#include <cstdio>
#include <algorithm>
using namespace std;
const int mod=1333331;
typedef long long LL;
LL a,b,miu[5000010];
int p[500010],cnt=0;
bool vis[5000010];
struct HASHMAP{
int h[mod+10],cnt,nxt[100010];
LL st[100010],S[100010];
void push(LL k,LL v){
int key=k%mod;
for(int i=h[key];i;i=nxt[i]){
if(S[i]==k)return;
}++cnt;nxt[cnt]=h[key];h[key]=cnt;
S[cnt]=k;st[cnt]=v;
}
LL ask(LL k){
int key=k%mod;
for(int i=h[key];i;i=nxt[i]){
if(S[i]==k)return st[i];
}return -1;
}
}H;
void Get_Prime(){
miu[1]=1;
for(int i=2;i<=5000000;++i){
if(!vis[i]){p[++cnt]=i;miu[i]=-1;}
for(int j=1;j<=cnt;++j){
if(1LL*p[j]*i>5000000)break;
int ip=i*p[j];
vis[ip]=true;
if(i%p[j]==0)break;
miu[ip]=-miu[i];
}
}for(int i=2;i<=5000000;++i)miu[i]+=miu[i-1];
}
LL miu_sum(LL n){
if(n<=5000000)return miu[n];
LL tmp=H.ask(n),la,A=1;
if(tmp!=-1)return tmp;
for(LL i=2;i<=n;i=la+1){
LL now=n/i; la=n/now;
A=A-(la-i+1)*miu_sum(n/i);
}H.push(n,A);return A;
}
int main(){
scanf("%lld%lld",&a,&b);
Get_Prime();
printf("%lld\n",miu_sum(b)-miu_sum(a-1));
return 0;
}
51nod 1244 莫比乌斯函数之和(杜教筛)的更多相关文章
- 51Nod.1244.莫比乌斯函数之和(杜教筛)
题目链接 map: //杜教筛 #include<map> #include<cstdio> typedef long long LL; const int N=5e6; in ...
- 51 NOD 1244 莫比乌斯函数之和(杜教筛)
1244 莫比乌斯函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens) ...
- 【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛
题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod. ...
- 51nod1244 莫比乌斯函数之和 杜教筛
虽然都写了,过也过了,还是觉得杜教筛的复杂度好玄学 设f*g=h,∑f=S, 则∑h=∑f(i)S(n/i下取整) 把i=1时单独拿出来,得到 S(n)=(∑h-∑2->n f(i)S(n/i下 ...
- 51nod 1244 莫比乌斯函数之和 【杜教筛】
51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...
- [51Nod 1244] - 莫比乌斯函数之和 & [51Nod 1239] - 欧拉函数之和 (杜教筛板题)
[51Nod 1244] - 莫比乌斯函数之和 求∑i=1Nμ(i)\sum_{i=1}^Nμ(i)∑i=1Nμ(i) 开推 ∑d∣nμ(d)=[n==1]\sum_{d|n}\mu(d)=[n== ...
- 51nod 1244 莫比乌斯函数之和
题目链接:51nod 1244 莫比乌斯函数之和 题解参考syh学长的博客:http://www.cnblogs.com/AOQNRMGYXLMV/p/4932537.html %%% 关于这一类求积 ...
- [51Nod 1237] 最大公约数之和 (杜教筛+莫比乌斯反演)
题目描述 求∑i=1n∑j=1n(i,j) mod (1e9+7)n<=1010\sum_{i=1}^n\sum_{j=1}^n(i,j)~mod~(1e9+7)\\n<=10^{10}i ...
- 【51nod】1239 欧拉函数之和 杜教筛
[题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...
随机推荐
- SSH整合,applicationContext.xml中配置hibernate映射文件问题
今天在applicationContext.xml中配置sessionFactory时遇到了各种头疼的问题,现在总结一下: 1.<property name="mappingDirec ...
- Matlab图像直方图相关函数
图像的灰度直方图(H是图像a.bmp的数据矩阵) imhist(H):%显示a的直方图 histeq(H); %将图像a进行直方图均衡化 adapthisteq(H); %将图像a进行直方图均衡化 i ...
- 搜索服务器xunsearch实现
安装方法: centos 6.6 64位 histroy: 12 cd /srv/ 13 wget http://www.xunsearch.com/download/xunsea ...
- mongodb or操作与连接池
mongodb # 类似于sql中的in或者or操作 mulites field query: db.cool.find({$or:[{field1:'val'},{'field2':'val'}-] ...
- Apache commons (Java常用工具包)简介
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题,下面是一个简述表,详细信息访问http://jakarta.apache.org/commons/index.html Be ...
- Cloning Java objects using serialization
Sometimes you need to clone objects, and sometimes you can't use their clone method, and sometimes s ...
- C语言入门(10)——if分支语句
在我们写的函数中可以有多条语句,但这些语句总是从前到后顺序执行的.除了从前到后顺序执行之外,有时候我们需要检查一个条件,然后根据检查的结果执行不同的后续代码,在C语言中可以用分支语句实现,比如: if ...
- Openstack service default port
Block Storage (cinder) 8776 publicurl and adminurl Compute API (nova-api) 8773 EC2 API 8774 openstac ...
- python手记(9)
本博客所有内容是原创,未经书面许可,严禁任何形式的转 http://blog.csdn.net/u010255642 tab #!/usr/bin/env python # example noteb ...
- android样式跟主题
简单说类似与自定义控件,只不过自定义控件针对的是view 而样式与主题针对的是属性.元素 在TexvView中引入样式 layout.xml <?xml version="1.0&qu ...