莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出。梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号。具体定义如下:
如果一个数包含平方因子,那么miu(n) = 0。例如:miu(4), miu(12), miu(18) = 0。
如果一个数不包含平方因子,并且有k个不同的质因子,那么miu(n) = (-1)^k。例如:miu(2), miu(3), miu(30) = -1,miu(1), miu(6), miu(10) = 1。
 
给出一个区间[a,b],S(a,b) = miu(a) + miu(a + 1) + ...... miu(b)。
例如:S(3, 10) = miu(3) + miu(4) + miu(5) + miu(6) + miu(7) + miu(8) + miu(9) + miu(10)
= -1 + 0 + -1 + 1 + -1 + 0 + 0 + 1 = -1。
Input
输入包括两个数a, b,中间用空格分隔(2 <= a <= b <= 10^10)
Output
输出S(a, b)。
Input示例
3 10
Output示例
-1
#include<cstdio>
#include<iostream>
#define N 2000010
#define mod 2333333
#define lon long long
#ifdef unix
#define LL "%lld"
#else
#define LL "%I64d"
#endif
using namespace std;
int pri[N],num,mul[N],mark[N],sum[N],head[mod+],cnt;
struct node{
lon to;int pre,val;
};node e[N];
void get_prime(){
mul[]=;
for(int i=;i<N;i++){
if(!mark[i]) pri[++num]=i,mul[i]=-;
for(int j=;j<=num&&i*pri[j]<N;j++){
mark[i*pri[j]]=;
mul[i*pri[j]]=-mul[i];
if(i%pri[j]==){mul[i*pri[j]]=;break;}
}
}
}
void add(int u,lon v,int val){
e[++cnt].to=v;
e[cnt].val=val;
e[cnt].pre=head[u];
head[u]=cnt;
}
int solve(lon x){
if(x<N) return sum[x];
int y=x%mod,r=;
for(int i=head[y];i;i=e[i].pre)
if(e[i].to==x) return e[i].val;
lon last;
for(lon i=;i<x;i=last+){
last=(x/(x/i));
r-=solve(x/i)*(last-i+);
}
add(y,x,r);
return r;
}
int main(){
lon a,b;scanf(LL LL,&a,&b);
get_prime();
for(int i=;i<N;i++) sum[i]=sum[i-]+mul[i];
printf("%d",solve(b)-solve(a-));
return ;
}

莫比乌斯函数之和(51nod 1244)的更多相关文章

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

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

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

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

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

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

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

  5. 51nod1244 莫比乌斯函数之和

    推公式.f[n]=1-∑f[n/i](i=2...n).然后递归+记忆化搜索.yyl说这叫杜教筛?时间复杂度貌似是O(n 2/3)的? #include<cstdio> #include& ...

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

    [题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...

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

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

  8. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  9. 【51nod】1244 莫比乌斯函数之和

    题解 求积性函数的前缀和?杜教筛! 这不给一发杜教筛入门必备之博客= = https://blog.csdn.net/skywalkert/article/details/50500009 好了,然后 ...

随机推荐

  1. JavaScript——图片懒加载

    前言 有一个朋友问我这个问题,刚好有时间,现在就简单的写个Demo~ github | https://github.com/wangyang0210/bky/tree/picLoadLazy 内容 ...

  2. 06grep与find命令详解

    1. grep 命令 grep 命令用于在文本中执行关键词搜索,并显示匹配的结果,格式为"grep [选项][文件]". grep 命令的参数及其作用如下: -b 将可执行文件(b ...

  3. destoon模块自定义字段的添加并让其支持搜索的方法

    今天看了看模块设置里的自定义字段功能的用法,试着加了个新字段glry,设置了值,然后去数据库moduleid的article表看,字段成功加上了. 于是去template下article文件夹的lis ...

  4. 无需上传附件到服务器,Servlet读取Excel(二)

    package com.str; import java.io.File;import java.io.FileInputStream;import java.io.IOException; impo ...

  5. graph-bfs-八数码问题

    这个看起来是童年回忆:) 大体思路是,将每个排列状态看成图中的一个点,状态之间转换说明有边.然后用bfs,如果遍历完之后还是没有找到目标状态, 则说明是无解的,否则输出步数.具体想法写在代码里吧,多多 ...

  6. 使用powershell/vbs自动化模拟鼠标点击操作

    今天想做windows上的自动化,所以才有了模拟鼠标点击的需求,先考虑用powershell实现: 首先先安装一个名为“WASP”免费可用的Powershell扩展程序,下载地址:http://was ...

  7. AD转换器的参数介绍

    分辨率.参考电压这些地球人都知道的就不说了. 当“参考电压”和“分辨率”被确定后,每两个数值间的差值,即“步进量”. 上面的“步进量”在AD中称为1LSB(最低有效位,Least Significan ...

  8. BZOJ 4479: [Jsoi2013]吃货jyy

    一句话题意:求必须包含某K条边的回路(回到1),使得总权值最小 转化为权值最小的联通的偶点 令F[i]表示联通状态为i的最小权值,(3^n状压)表示不在联通块内/奇点/偶点,连边时先不考虑必选的边的度 ...

  9. BZOJ 4057: [Cerc2012]Kingdoms

    状压DP #include<cstdio> #include<cstring> using namespace std; int F[1200005],A[25][25],st ...

  10. 关于 NSData 的数据类型(2进制,16进制之间)及深入剖析

    1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc initWithData:adataenc ...