【题目链接】

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 莫比乌斯函数之和(杜教筛)的更多相关文章

  1. 51Nod.1244.莫比乌斯函数之和(杜教筛)

    题目链接 map: //杜教筛 #include<map> #include<cstdio> typedef long long LL; const int N=5e6; in ...

  2. 51 NOD 1244 莫比乌斯函数之和(杜教筛)

    1244 莫比乌斯函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens) ...

  3. 【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛

    题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod. ...

  4. 51nod1244 莫比乌斯函数之和 杜教筛

    虽然都写了,过也过了,还是觉得杜教筛的复杂度好玄学 设f*g=h,∑f=S, 则∑h=∑f(i)S(n/i下取整) 把i=1时单独拿出来,得到 S(n)=(∑h-∑2->n f(i)S(n/i下 ...

  5. 51nod 1244 莫比乌斯函数之和 【杜教筛】

    51nod 1244 莫比乌斯函数之和 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含 ...

  6. [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== ...

  7. 51nod 1244 莫比乌斯函数之和

    题目链接:51nod 1244 莫比乌斯函数之和 题解参考syh学长的博客:http://www.cnblogs.com/AOQNRMGYXLMV/p/4932537.html %%% 关于这一类求积 ...

  8. [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 ...

  9. 【51nod】1239 欧拉函数之和 杜教筛

    [题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...

随机推荐

  1. Linux 学习之防火墙配置

    1.安装iptables防火墙 yum install iptables  2. 清除已有的iptables规则  iptables -F  iptables -X  iptables -Z  3.显 ...

  2. 写一个jq插件

    本文章摘自博客园的http://www.cnblogs.com/JustinYoung/archive/2010/03/30/jquery-chajian.html,写此文章只是方便自己记载技术 一个 ...

  3. ajax调用后台Java

    //html部分 <input type='text' placeholder='用户名' id="username" name="username" c ...

  4. English interview!

    Q1:Why are you interested in working for our company?为什么有兴趣在我们公司工作?A1:Because your company has a goo ...

  5. Python Tensorflow下的Word2Vec代码解释

    前言: 作为一个深度学习的重度狂热者,在学习了各项理论后一直想通过项目练手来学习深度学习的框架以及结构用在实战中的知识.心愿是好的,但机会却不好找.最近刚好有个项目,借此机会练手的过程中,我发现其实各 ...

  6. 传智播客C/C++学院年薪24-50万招聘C/C++讲师

    C/C++技术讲师 6名 (北京,年薪:24-50万) 传智播客C/C++课程培训体系如下: 1.C语言,世界五百强C语言面试训练 2.C++语言,世界五百强C++语言面试训练 3.数据结构与算法,世 ...

  7. var 与function的权重浅析

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 【转】stdin, stdout, stderr 以及重定向

    详细见: http://my.oschina.net/qihh/blog/55308 stdin是标准输入文件,stdout是标准输出文件,stderr标准出错文件. 程序按如下方式使用这些文件: 标 ...

  9. 小Y的难题

    Font Size:Aa Aa Aa Description 近期小Y迷上了数学,总是在思考各种数学问题.有一天,他不小心把墨水洒在草稿纸上.他如今能看到的是"2?3?1?4"(? ...

  10. Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性

    新的keyword auto C++11中引入auto第一种作用是为了自己主动类型推导 auto的自己主动类型推导,用于从初始化表达式中判断出变量的数据类型.通过auto的自己主动类型推导.能够大大简 ...