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 ...
随机推荐
- leetcode Binary Tree Postorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- poco vs Boost[ZZ]
http://wooce.iteye.com/blog/634395 POCO的优点: 1) 比boost更好的线程库,特别是一个活动的方法的实现,并且还可设置线程的优先级. 2) 比 boost:a ...
- STL MAP 反序迭代
ITS_NOTICE_MAP::reverse_iterator it = noticeMap.rbegin(); for ( ; it != noticeMap.rend(); ++it ) { I ...
- ie6,ie7,ie8 css bug汇总以及兼容解决方法
1:li边距“无故”增加 任何事情都是有原因的,li边距也不例外. 先描述一下具体状况:有些时候li边距会突然增 加很多,值也不固定(只在IE6/IE7有这种现象),让人摸不着头脑,仔细“研究”发现是 ...
- mysql登录错误或者密码错误
一.mysql登录错误 mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root ...
- codeforces 659E . New Reform 强连通
题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...
- Perfmon 介绍
1.UI 1.笔图标,可以把视图里的细线加粗. 2.加号图标.可以为视图添加新的记数器. 3.操作-->属性.可以用于设置默认的采样时间. 2.增加计数据器页面 1.一个计数据器会包涵多个对象. ...
- Genymotion中SD卡目录在Eclipse中查看,以及创建SDCard
咦?这后面似乎指向一个目录,我就去找/mnt/shell/emulated/0 Wow~好熟悉的目录..不熟悉的同学可以打开android模拟器的File Manger App 里面就是这些目录了,然 ...
- STL中用erase()方法遍历删除元素
STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...
- 详解Spring中的CharacterEncodingFilter--forceEncoding为true在java代码中设置失效--html设置编码无效
在项目中有很多让人头疼的问题,其中,编码问题位列其一,那么在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?下面我们来看看Spring框架给我们提供过滤器CharacterEncodin ...