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 Merge K sorted Lists python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- C++ 获取UUID
#include <string> #include <stdio.h> #if defined(WIN32)||defined(WINCE)||defined(WIN64) ...
- Android 树形菜单
首先来一张萌萌哒的效果图(比较懒 - -) 然后是代码: // Node package com.example.treeview.utils; import java.util.ArrayList; ...
- select * from (select P.*,ROWNUM RN FROM(select * from Mp_Relatedart where pubbaseid=785 order by ID ASC )P)M WHERE M.RN>2 and M.RN <= 7
select * from (select P.*,ROWNUM RN FROM(select * from Mp_Relatedart where pubbaseid=785 order by ID ...
- Android:res之selector背景选择器
selector根据不同的选定状态来定义不同的现实效果 常用属性: android:state_selected--------选中android:state_focused--------获得焦点a ...
- css3中动画animation的应用
<!DOCTYPE html> <html> <head> <style> /* @-webkit-keyframes anim1 { // 规定动画. ...
- python相似模块用例(一)
一:threading VS Thread 众所周知,python是支持多线程的,而且是native的线程,其中threading是对Thread模块做了包装,可以更加方面的被使用,threading ...
- SilverLight搭建WCF聊天室详细过程
收藏SL双工通信例子教程 SilverLight 4正式版发布给开发人员带来了更多功能,并且4已经支持NET.TCP协议,配合WCF开发高效率的交互应用程序已经不再是难事,本系列文章主要针对已经完成的 ...
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...