1244 莫比乌斯函数之和

基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注

莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出。梅滕斯(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<iostream>
#include<cstdio>
#define MAXN 2000001
#define mod 1333333
#define LL long long
using namespace std;
int mu[MAXN],p[MAXN],cut,pri[MAXN],tot,head[MAXN],sum[MAXN];
LL l,r;
struct data{int next;int v,x;}e[MAXN];
bool vis[MAXN];
void add(int u,int v,int x)
{
e[++cut].v=v;e[cut].x=x;e[cut].next=head[u];head[u]=cut;
}
void pre()
{
mu[1]=1;
for(int i=2;i<=MAXN-1;i++)
{
if(!vis[i]) vis[i]=true,pri[++tot]=i,mu[i]=-1;
for(int j=1;j<=tot&&i*pri[j]<=MAXN-1;j++)
{
if(!vis[i*pri[j]]) vis[i*pri[j]]=true;
if(i%pri[j]) mu[i*pri[j]]=-mu[i];
else {mu[i*pri[j]]=0;break ;}
}
}
for(int i=1;i<=MAXN-1;i++) sum[i]=sum[i-1]+mu[i];
return ;
}
LL slove(LL x)
{
if(x<MAXN) return sum[x];
int ans=0;LL k=x%mod,last;
for(int i=head[k];i;i=e[i].next)
if(e[i].v==x) return e[i].x;
for(LL i=2;i<=x;i=last+1)
{
last=x/(x/i);
ans+=(last-i+1)*slove(x/i);
}
ans=1-ans;
add(k,x,ans);
return ans;
}
int main()
{
pre();
cin>>l>>r;
cout<<slove(r)-slove(l-1);
return 0;
}

最新文章

  1. appserv中php升级问题
  2. Feature hashing相关 - 1
  3. [CareerCup] 18.2 Shuffle Cards 洗牌
  4. php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
  5. 使用Angular和Nodejs搭建聊天室
  6. Servlet 3特性:异步Servlet
  7. GRID用法(取行、列值;定位选中某行等等)
  8. [Android]AndroidDesign中ActionBar探究1
  9. [cocos2dx 3.x]Label类数字变化动作
  10. hdu 1050 Moving Tables
  11. 系统自带.net版本
  12. vs2010-error LNK1123: failure during conversion to COFF: file invalid or corrupt
  13. Visual Studio 我的插件
  14. [ZJOI2009]染色游戏
  15. Google工程师巩朋的算法之路(1)
  16. em,px,rem的区别
  17. (转) HighCharts 非规律日期 多条曲线的 绘画
  18. Tarjan+缩点【强连通分量】【模板】
  19. Alpha冲刺阶段博客汇总
  20. 内网安全监控和预警平台架构设想(OSSIM)

热门文章

  1. 全面优化MySQL
  2. 『Python基础练习题』day03
  3. io.lettuce.core.protocol.ConnectionWatchdog - Reconnecting, last destination was ***
  4. @app.route源码流程分析
  5. iOS - The file “XXX.app” couldn’t be opened because you don’t have permission to view it.
  6. 快速提交一个项目到github或gitee上
  7. iOS CGContextRef/UIBezierPath(绘图)
  8. impala 表迁移方式 partquet数据文件移动方法
  9. 关于MUI页面之间传值以及刷新的问题
  10. 元组和range