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

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ll long long
#define qwq(x) for(edge *o=head[x];o;o=o->next)
const int mod=1e6+7;
const int nmax=7e6+5;
struct edge{
int to,dis;edge *next;
};
edge es[mod],*pt=es,*head[nmax];
int mo[nmax+1],pe[nmax];bool vis[nmax+1];
void add(int u,int v,int d){
pt->to=v;pt->dis=d;pt->next=head[u];head[u]=pt++;
}
int get(ll n){
if(n<=nmax) return mo[n];
int tp=n%mod;
qwq(tp) if(o->to==n) return o->dis;
ll last,ans=0;
for(ll i=2;i<=n;i=last+1){
last=n/(n/i);
ans+=(last-i+1)*get(n/i);
}
add(tp,n,1-ans);
return 1-ans;
}
int main(){
mo[1]=1;int cnt=0,tp;
rep(i,2,nmax){
if(!vis[i]) pe[++cnt]=i,mo[i]=-1;
rep(j,1,cnt){
tp=pe[j];if(tp*i>nmax) break;vis[i*tp]=1;
if(i%tp==0){
mo[i*tp]=0;break;
} mo[i*tp]=-mo[i];
}
}
rep(i,1,nmax) mo[i]+=mo[i-1];
ll n,m;scanf("%lld%lld",&n,&m);
printf("%d\n",get(m)-get(n-1));
return 0;
}

  

基准时间限制: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

51nod1244 莫比乌斯函数之和的更多相关文章

  1. 51nod1244 莫比乌斯函数之和(杜教筛)

    题面 传送门 题解 我--我忘记把预处理的块的大小调成\(n^{\frac{2}{3}}\)了--(仰天) 首先\(\mu*1=e\) 然后杜教筛就行了 //minamoto #include< ...

  2. 51nod1244 莫比乌斯函数之和 杜教筛

    虽然都写了,过也过了,还是觉得杜教筛的复杂度好玄学 设f*g=h,∑f=S, 则∑h=∑f(i)S(n/i下取整) 把i=1时单独拿出来,得到 S(n)=(∑h-∑2->n f(i)S(n/i下 ...

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

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

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

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

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

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

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

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

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

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

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

  9. 莫比乌斯函数之和(51nod 1244)

    莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含平方因子,那么miu(n) = 0.例如 ...

随机推荐

  1. Ruby中的Profiling工具

    看看如何调试Ruby的性能问题 李哲 - APRIL 08, 2015 Ruby内置的profiler 内置的profiler实现的很简单,在ruby2.2中只有150行代码,大家可以看看它的实现pr ...

  2. JavaScript创建命名空间、类及类成员

    JavaScript代码: //为String对象增加方法isNullOrEmpty String.isNullOrEmpty = function (a) { if (a != null & ...

  3. POJ 3484

    Showstopper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1060   Accepted: 303 Descri ...

  4. SQL技术内幕-12 SQL优化方法论前言

    我推荐的一种使用自顶向下的优化论.这种方法,首先分析实例级的等待时间,在通过一系列步骤将其不断细化,知道找出系统中导致大量等待的进程/组件.一旦找出这些令人讨厌的进程,就可以集中优化他们了,一下是这种 ...

  5. [你必须知道的.NET]第三十四回,object成员,不见了!

    发布日期:2009.10.30 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 在.NET世界了,object是公认的造物主,其麾下的7大成员, ...

  6. 【转】VC++与MySQL数据库的连接

    原文地址:http://blog.csdn.net/nupt123456789/article/details/8043091 1.MySQL数据库的安装 你可以从MySQL的官网上或者从如下地址下载 ...

  7. lintcode:将二叉树拆成链表

    题目 将一棵二叉树按照前序遍历拆解成为一个假链表.所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针. 注意事项 不要忘记将左儿子标记为 null,否则你可能会得到空间溢出 ...

  8. IOS开发中的几种设计模式

    ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~ (一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则 ...

  9. Win8.1安装VirtualSVN Server发生service visualSVN Server failed to start解决办法

    Service 'VisualSVN Server' failed to start. Please check VisualSVN Server log in Event Viewer for mo ...

  10. MAVEN ERROR: unable to find valid certification path to requested target 解决办法

    第一次使用MAVEN编译项目,出现如下错误 解决办法:Maven的setting.xml中添加如下代码 <mirrors> <mirror> <id>Central ...