题意:

求解$\sum_{i=a}^b{\mu(i)}$。

解法:

由$(\mu * I)(n) = e(n)$ 得 $\sum_{d|n}{\mu(d)} = [n=1]$ 得 $\mu(n) = \sum_{d|n,d<n}{\mu(d)}$

从而有$$\sum_{i=1}^n{\mu(i)} = 1 - \sum_{i=1}^n{ \sum_{d|i,d<i}{\mu(d)} }$$

    $$=1-\sum_{t=2}^n{ \sum_{d=1}^{[\frac{n}{t}]}{\mu(d)} }$$

记$S(n) = \sum_{i=1}^n{\mu(i)}$

从而有$S(n) = 1- \sum_{t=2}^n{S([\frac{n}{t}])}$

考虑分块优化此式,产生$O(\sqrt n)$的时间复杂度,当n小于等于$n^{0.6667}$时直接应用线性筛计算。

分析得会产生$O(n^{0.667})$个n,从而应用map,递归计算即可。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <map>
  6.  
  7. #define LL long long
  8. #define LIM 5000000
  9.  
  10. using namespace std;
  11.  
  12. int tot,prime[LIM+];
  13. LL u[LIM+];
  14. bool v[LIM+];
  15. map<LL,LL> ansv;
  16.  
  17. LL S(LL n)
  18. {
  19. if(n<=LIM) return u[n];
  20. if(ansv.count(n)) return ansv[n];
  21. LL j;
  22. LL ans=;
  23. for(LL i=;i<=n;i=j+)
  24. {
  25. j=n/(n/i);
  26. ans -= (j-i+1LL) * S(n/i);
  27. }
  28. ansv[n]=ans;
  29. return ans;
  30. }
  31.  
  32. int main()
  33. {
  34. // freopen("test.txt","r",stdin);
  35. u[]=;
  36. for(int i=;i<=LIM;i++)
  37. {
  38. if(!v[i])
  39. {
  40. prime[++tot]=i;
  41. u[i]=-;
  42. }
  43. for(int j=;i*prime[j]<=LIM;j++)
  44. {
  45. v[i*prime[j]]=;
  46. u[i*prime[j]]=u[i]*u[prime[j]];
  47. if(i%prime[j]==)
  48. {
  49. u[i*prime[j]]=;
  50. break;
  51. }
  52. }
  53. }
  54. for(int i=;i<=LIM;i++) u[i]+=u[i-];
  55. LL a,b;
  56. cin >> a >> b;
  57. cout << S(b)-S(a-) << endl;
  58. return ;
  59. }

同样的方法,由$(\phi * I)(n) = id(n)$得到

$S(n) = \frac{n(n+1)}{2} - \sum_{t=2}^n{S([\frac{n}{t}])}$

注意n*(n+1)可能炸long long。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <map>
  6. #include <cassert>
  7.  
  8. #define LL long long
  9. #define LIM 5000000
  10. #define P 1000000007LL
  11.  
  12. using namespace std;
  13.  
  14. int tot,prime[LIM+];
  15. LL phi[LIM+],inv2;
  16. bool v[LIM+];
  17. map<LL,LL> ansv;
  18.  
  19. LL S(LL n)
  20. {
  21. if(n<=LIM) return phi[n];
  22. if(ansv.count(n)) return ansv[n];
  23. LL j;
  24. LL ans=n%P * (n%P + 1LL) %P * inv2%P;
  25. assert(ans >=);
  26. for(LL i=;i<=n;i=j+)
  27. {
  28. j=n/(n/i);
  29. ans += P - ((j-i+1LL) * S(n/i)%P);
  30. if(ans>=P) ans -= P;
  31. }
  32. ansv[n]=ans;
  33. return ans;
  34. }
  35.  
  36. LL qpow(LL x,int n)
  37. {
  38. LL ans=;
  39. for(;n;n>>=,x=x*x%P) if(n&) ans=ans*x%P;
  40. return ans;
  41. }
  42.  
  43. int main()
  44. {
  45. // freopen("test.txt","r",stdin);
  46. phi[]=;
  47. for(int i=;i<=LIM;i++)
  48. {
  49. if(!v[i])
  50. {
  51. prime[++tot]=i;
  52. phi[i]=i-;
  53. }
  54. for(int j=;i*prime[j]<=LIM;j++)
  55. {
  56. v[i*prime[j]]=;
  57. phi[i*prime[j]]=phi[i]*phi[prime[j]];
  58. if(i%prime[j]==)
  59. {
  60. phi[i*prime[j]]=phi[i]*prime[j];
  61. break;
  62. }
  63. }
  64. }
  65. inv2=qpow(,P-);
  66. for(int i=;i<=LIM;i++)
  67. {
  68. phi[i]=phi[i]+phi[i-];
  69. if(phi[i]>=P) phi[i] -= P;
  70. }
  71. LL n;
  72. cin >> n;
  73. cout << S(n) << endl;
  74. return ;
  75. }

Mertens的更多相关文章

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

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

  2. 分析一个嵌入payload的恶意.lnk文件

    原文:https://isc.sans.edu/diary/Analyzis+of+a+Malicious+.lnk+File+with+an+Embedded+Payload/20763 We re ...

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

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

  4. ### Paper about Event Detection

    Paper about Event Detection. #@author: gr #@date: 2014-03-15 #@email: forgerui@gmail.com 看一些相关的论文. 1 ...

  5. 51nod1240莫比乌斯函数

    莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.(据说,高斯(Gauss)比莫比乌斯早三十年就曾考虑过这个函数).   ...

  6. 51nod--1240莫比乌斯函数 (数论)

    题目: 1240 莫比乌斯函数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先 ...

  7. 51 Nod 1240 莫比乌斯函数

    1240 莫比乌斯函数  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使 ...

  8. 51Nod 1240:莫比乌斯函数

    1240 莫比乌斯函数  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使 ...

  9. 使用开源软件 enfuse 做照片的曝光合成

    使用开源软件 enfuse 做照片的曝光合成 所谓曝光合成就是对同一场景用不同的曝光量拍摄多张照片,然后将这些照片再合成为一张照片.之所以我们要这么做是因为现在的相机感光的动态范围相比人眼实在是太小了 ...

随机推荐

  1. CrtmpServr 接收Http流程

    最近在研究CrtmpServer http部分,记录一些基本的流程,以备查阅. 首先,打开配置脚本CrtmpServer.lua ,确认脚本中有以下内容,如果没有需要加上. { name=" ...

  2. POCO类

    我认为POCO(简单传统CLR对象)重点应该是简单,不跟其他不相关的类进行关联关系或不相关的属性.<NHibernate 4 Beginner Guid>这本书有提到,应该是满足下面三个条 ...

  3. C#除法精度

    string.empty()NULL 首先要安装虚拟机工具VMWare Tool这样鼠标进出使用也方便. 1.虚拟机和主机之间在安装了VMWare Tool之后可以实现剪贴板的共享,即可以复制粘贴.但 ...

  4. Java泛型 类型变量的限定

    有时候,类和方法须要对类型变量加以约束.比方你有一个方法,你仅仅希望它接收某个特定类型及其子类型作为參数. 以下就举一个方法限定接收參数的类型的样例来说明怎样限定类型变量. 首先有几个简单的辅助类: ...

  5. Canvas学习笔记——动画环境中的边界

    在动画中经常要处理边界问题,比如一个物体运动到了边界,要怎么处理才合适呢?通常有几种以下几种方式: 让物体消失 // > 16 & 0xff, g = color >> 8 ...

  6. c# SQLServer导入大批量数据

    说来惭愧,关于批量导入数据,一直采用的是最原始的方式,一条一条插入,或者100条一块批量插入,这种方式,五十步笑百步,并没有明显的性能提升, 昨天在从别的库查询数据到DataTable内存中,然后插入 ...

  7. poj 2154 Color < 组合数学+数论>

    链接:http://poj.org/problem?id=2154 题意:给出两个整数 N 和 P,表示 N 个珠子,N种颜色,要求不同的项链数, 结果 %p ~ 思路: 利用polya定理解~定理内 ...

  8. Struts2中的OGNL表达式

    一.OGNL表达式简介 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目.所谓对象图,即以任意一个对象为根,通过OGNL可以访问 ...

  9. hsv hsb rgb lab

    lab  欧式距离 反映 人类所能感受到的这两种颜色的差异

  10. Avro Parquet

    行   支持数据追加 列  频繁进行小部分列查询