莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出。梅滕斯(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
  1. 输入包括两个数a, b,中间用空格分隔(2 <= a <= b <= 10^10)
Output
  1. 输出S(a, b)。
Input示例
  1. 3 10
Output示例
  1. -1
  1. #include<cstdio>
  2. #include<iostream>
  3. #define N 2000010
  4. #define mod 2333333
  5. #define lon long long
  6. #ifdef unix
  7. #define LL "%lld"
  8. #else
  9. #define LL "%I64d"
  10. #endif
  11. using namespace std;
  12. int pri[N],num,mul[N],mark[N],sum[N],head[mod+],cnt;
  13. struct node{
  14. lon to;int pre,val;
  15. };node e[N];
  16. void get_prime(){
  17. mul[]=;
  18. for(int i=;i<N;i++){
  19. if(!mark[i]) pri[++num]=i,mul[i]=-;
  20. for(int j=;j<=num&&i*pri[j]<N;j++){
  21. mark[i*pri[j]]=;
  22. mul[i*pri[j]]=-mul[i];
  23. if(i%pri[j]==){mul[i*pri[j]]=;break;}
  24. }
  25. }
  26. }
  27. void add(int u,lon v,int val){
  28. e[++cnt].to=v;
  29. e[cnt].val=val;
  30. e[cnt].pre=head[u];
  31. head[u]=cnt;
  32. }
  33. int solve(lon x){
  34. if(x<N) return sum[x];
  35. int y=x%mod,r=;
  36. for(int i=head[y];i;i=e[i].pre)
  37. if(e[i].to==x) return e[i].val;
  38. lon last;
  39. for(lon i=;i<x;i=last+){
  40. last=(x/(x/i));
  41. r-=solve(x/i)*(last-i+);
  42. }
  43. add(y,x,r);
  44. return r;
  45. }
  46. int main(){
  47. lon a,b;scanf(LL LL,&a,&b);
  48. get_prime();
  49. for(int i=;i<N;i++) sum[i]=sum[i-]+mul[i];
  50. printf("%d",solve(b)-solve(a-));
  51. return ;
  52. }

莫比乌斯函数之和(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. 解决TS报错Property 'style' does not exist on type 'Element'

    在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...

  2. js中替换字符串

    function formatStr(str){ str=str.replace(/\r\n/ig,"<br/>"); return str; } 要注意两点: 要使用 ...

  3. 基于matlab的蓝色车牌定位与识别---定位

    接着昨天的工作继续.定位的过程有些是基于车牌的颜色进行定位的,自己则根据数字图像一些形态学的方法进行定位的. 合着代码进行相关讲解. 1.相对彩色图像进行灰度化,然后对图像进行开运算.再用小波变换获取 ...

  4. windows server 服务器 环境配置

    自动备份 xcopy d:\web\zhiku\*.* d:\bak\web\zhiku\%date:~,4%%date:~5,2%%date:~8,2%\ /S /I

  5. 【linux】【进程】stand alone 与 super daemon 区别

    本文引用自  鸟哥的linux私房菜如果依据 daemon 的启动与管理方式来区分,基本上,可以将 daemon 分为可独立启动的 stand alone , 与透过一支 super daemon 来 ...

  6. html5音频audio对象处理以及ios微信端自动播放和息屏后唤醒的判断---可供参考(功能都完整实现了,只是细节还没处理的很好)

    // html模版中的 此处结合了weui样式整合的微信手机端片段代码(不可直接粘贴复制进行使用)里面含有一些php的写法,可直接略过..###重点参考js代码### <div> < ...

  7. Mr. Panda and Crystal HDU - 6007 最短路+完全背包

    题目:题目链接 思路:不难看出,合成每个宝石需要消耗一定的魔力值,每个宝石有一定的收益,所以只要我们知道每个宝石合成的最小花费,该题就可以转化为一个背包容量为初始魔力值的完全背包问题,每个宝石的最小花 ...

  8. 水题:UVa489-Hangman Judge

    Hangman Judge Time limit 3000 ms Description In Hangman Judge, you are to write a program that judge ...

  9. JS实现——贪吃蛇

    把以下代码保存成Snake.html文件,使用Google或360浏览器打开 <!DOCTYPE HTML> <html> <head> <meta char ...

  10. ogre的初始化与启动以及显示对象设置

    ogre的使用方法1---自动设置 1.ogre初始化:首先实例化一个Root对象 Root * root = new Root(); Root * root = new Root("plu ...