和bzoj 3944比较像,但是时间卡的更死

设\( f(n)=\sum_{d|n}\mu(d) g(n)=\sum_{i=1}^{n}f(i) s(n)=\sum_{i=1}^{n}\mu(i) \),然后很显然对于mu\( g(n)=1\),对于\( g(n)=n*(n+1)/2 \),然后可以这样转化一下:

\[g(n)=\sum_{i=1}^{n}\sum_{d|n}\mu(d)
\]

\[=\sum_{d=1}^{n}\mu(d)\left \lfloor \frac{n}{d} \right \rfloor
\]

\[=\sum_{d=1}^{n}s(\left \lfloor \frac{n}{d} \right \rfloor)
\]

\[s(n)=g(n)-\sum_{d=2}^{n}s(\left \lfloor \frac{n}{d} \right \rfloor)
\]

然后递归求解即可。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. const long long N=5000005,m=4500000;
  6. long long mb[N],tot,q[N],p[N];
  7. long long l,r;
  8. bool v[N];
  9. long long getp(long long x,long long n)
  10. {
  11. return (x<=m)?mb[x]:p[n/x];
  12. }
  13. void slv(long long x,long long n)
  14. {
  15. if(x<=m)
  16. return;
  17. long long t=n/x;
  18. if(v[t])
  19. return;
  20. v[t]=1;
  21. p[t]=1;
  22. for(long long i=2,la;la<x;i=la+1)
  23. {
  24. la=x/(x/i);
  25. slv(x/i,n);
  26. p[t]-=getp(x/i,n)*(la-i+1);
  27. }
  28. }
  29. long long wk(long long n)
  30. {
  31. if(n<=m)
  32. return mb[n];
  33. memset(v,0,sizeof(v));
  34. slv(n,n);
  35. return p[1];
  36. }
  37. int main()
  38. {
  39. mb[1]=1;
  40. for(long long i=2;i<=m;i++)
  41. {
  42. if(!v[i])
  43. {
  44. q[++tot]=i;
  45. mb[i]=-1;
  46. }
  47. for(long long j=1;j<=tot&&i*q[j]<=m;j++)
  48. {
  49. long long k=i*q[j];
  50. v[k]=1;
  51. if(i%q[j]==0)
  52. {
  53. mb[k]=0;
  54. break;
  55. }
  56. mb[k]=-mb[i];
  57. }
  58. }
  59. for(long long i=1;i<=m;i++)
  60. mb[i]+=mb[i-1];
  61. scanf("%lld%lld",&l,&r);
  62. printf("%lld\n",wk(r)-wk(l-1));
  63. return 0;
  64. }

51nod 1244 莫比乌斯函数之和 【莫比乌斯函数+杜教筛】的更多相关文章

  1. 51nod 1237 最大公约数之和 V3(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...

  2. 【luogu3768】简单的数学题 欧拉函数(欧拉反演)+杜教筛

    题目描述 给出 $n$ 和 $p$ ,求 $(\sum\limits_{i=1}^n\sum\limits_{j=1}^nij\gcd(i,j))\mod p$ . $n\le 10^{10}$ . ...

  3. Wannafly Camp 2020 Day 3D 求和 - 莫比乌斯反演,整除分块,STL,杜教筛

    杜教筛求 \(\phi(n)\), \[ S(n)=n(n+1)/2-\sum_{d=2}^n S(\frac{n}{d}) \] 答案为 \[ \sum_{d=1}^n \phi(d) h(\fra ...

  4. [51nod1237] 最大公约数之和 V3(杜教筛)

    题面 传送门 题解 我好像做过这题-- \[ \begin{align} ans &=\sum_{i=1}^n\sum_{j=1}^n\gcd(i,j)\\ &=\sum_{d=1}^ ...

  5. [51nod1238] 最小公倍数之和 V3(杜教筛)

    题面 传送门 题解 懒了--这里写得挺好的-- //minamoto #include<bits/stdc++.h> #define R register #define ll long ...

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

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

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

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

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

    题目链接 map: //杜教筛 #include<map> #include<cstdio> typedef long long LL; const int N=5e6; in ...

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

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

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

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

随机推荐

  1. Swift 入门学习一:简单值

    1.简单值 使用“let”来声明常量,使用“var”来声明变量. 常量,在编译的时候,并不需要有明确的值,但是只能赋值一次.即:可以用常量来表示这样一个值--只需要决定一次,但是需要使用很多次. va ...

  2. poj——3177Redundant Paths

    poj——3177Redundant Paths      洛谷—— P2860 [USACO06JAN]冗余路径Redundant Paths Time Limit: 1000MS   Memory ...

  3. Java处理XSS漏洞的工具类代码

    原文:http://www.open-open.com/code/view/1455809388308 public class AntiXSS { /** * 滤除content中的危险 HTML ...

  4. php.ini中extension默许的地址到底在哪里设置的

    原文: http://www.myexception.cn/php/1436096.html ----------------------------------------------------- ...

  5. 类的相互依赖导致StackOverflowError

    public class SchoolServiceImpl { private static SchoolServiceImpl instance = new SchoolServiceImpl() ...

  6. POJ 1679 The Unique MST 推断最小生成树是否唯一

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22715   Accepted: 8055 D ...

  7. eclipse Alt+/ 不能提示

    普通情况下alt+/有代码提示作用,还有代码提示的快捷代码也不是alt+/,因此要恢复代码提示用alt+/.须要做两件事.  在 Window - Preferences - General - Ke ...

  8. 碰撞检測之Sphere-Box检測

    检測思路 首先要做的是将Box转为AABB,然后推断圆心是否在Box内.用的就是之前的SAT 假设圆心在Box内,肯定相交, 假设不在圆心内.则有四种情况,与顶点相交,与楞相交,与面相交,这里的确定也 ...

  9. Android自己定义之TextView跑马灯的监听

    TextView都有跑马灯的效果,假设说让你去监听跑马灯效果的运行.我认为这个需求有点二了.可是也要实现. 思路: 1.自己定义View  继承TextView   这样的方法过于麻烦,仅仅是监听一个 ...

  10. Linux下通过find命令进行rm文件删除的小技巧

       我们常常会通过find命令进行批量操作.如:批量删除旧文件.批量改动.基于时间的文件统计.基于文件大小的文件统计等.在这些操作其中,因为rm删除操作会导致文件夹结构变化,假设要通过find结合r ...