51 NOD 1244 莫比乌斯函数之和(杜教筛)
2024-08-27 09:51:54
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;
}
最新文章
- appserv中php升级问题
- Feature hashing相关 - 1
- [CareerCup] 18.2 Shuffle Cards 洗牌
- php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
- 使用Angular和Nodejs搭建聊天室
- Servlet 3特性:异步Servlet
- GRID用法(取行、列值;定位选中某行等等)
- [Android]AndroidDesign中ActionBar探究1
- [cocos2dx 3.x]Label类数字变化动作
- hdu 1050 Moving Tables
- 系统自带.net版本
- vs2010-error LNK1123: failure during conversion to COFF: file invalid or corrupt
- Visual Studio 我的插件
- [ZJOI2009]染色游戏
- Google工程师巩朋的算法之路(1)
- em,px,rem的区别
- (转) HighCharts 非规律日期 多条曲线的 绘画
- Tarjan+缩点【强连通分量】【模板】
- Alpha冲刺阶段博客汇总
- 内网安全监控和预警平台架构设想(OSSIM)
热门文章
- 全面优化MySQL
- 『Python基础练习题』day03
- io.lettuce.core.protocol.ConnectionWatchdog - Reconnecting, last destination was ***
- @app.route源码流程分析
- iOS - The file “XXX.app” couldn’t be opened because you don’t have permission to view it.
- 快速提交一个项目到github或gitee上
- iOS CGContextRef/UIBezierPath(绘图)
- impala 表迁移方式 partquet数据文件移动方法
- 关于MUI页面之间传值以及刷新的问题
- 元组和range