题解

写完上一道就开始写这个,大体上就是代码改了改而已= =

好吧,再推一下式子!

\(\sum_{i = 1}^{n}i = \sum_{i = 1}^{n}\sum_{d | i}\phi(d) = \sum_{i = 1}^{n}\sum_{d = 1}^{\lfloor \frac{n}{i} \rfloor} \phi(d) = \sum_{i = 1}^{n}S(\lfloor \frac{n}{i} \rfloor)\)

\(S(n) = \frac{n(n + 1)}{2} - \sum_{i = 2}^{n} S(\lfloor \frac{n}{i} \rfloor)\)

long long相乘会爆掉,前面的注意特判一下

代码

  1. #include <bits/stdc++.h>
  2. #define MAXN 10000005
  3. //#define ivorysi
  4. #define enter putchar('\n')
  5. #define space putchar(' ')
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. #define mp make_pair
  10. #define eps 1e-8
  11. #define mo 974711
  12. #define pii pair<int,int>
  13. using namespace std;
  14. typedef long long int64;
  15. typedef double db;
  16. template<class T>
  17. void read(T &res) {
  18. res = 0;char c = getchar();T f = 1;
  19. while(c < '0' || c > '9') {
  20. if(c == '-') f = -1;
  21. c = getchar();
  22. }
  23. while(c >= '0' && c <= '9') {
  24. res = res * 10 + c - '0';
  25. c = getchar();
  26. }
  27. res *= f;
  28. }
  29. template<class T>
  30. void out(T x) {
  31. if(x < 0) {putchar('-');x = -x;}
  32. if(x >= 10) {
  33. out(x / 10);
  34. }
  35. putchar('0' + x % 10);
  36. }
  37. const int MOD = 1000000007;
  38. struct node {
  39. int64 x;
  40. int next,v;
  41. }E[1000005];
  42. int head[mo + 5],sumE;
  43. int prime[5000005],tot,P[MAXN],phi[MAXN];
  44. bool nonprime[MAXN];
  45. int inc(int a,int b) {
  46. a = a + b;
  47. if(a >= MOD) a -= MOD;
  48. return a;
  49. }
  50. void add(int u,int64 x,int v) {
  51. E[++sumE].x = x;E[sumE].v = v;E[sumE].next = head[u];
  52. head[u] = sumE;
  53. }
  54. void Insert(int64 x,int v) {
  55. add(x % mo,x,v);
  56. }
  57. int Query(int64 x) {
  58. int u = x % mo;
  59. for(int i = head[u] ; i ; i = E[i].next) {
  60. if(E[i].x == x) return E[i].v;
  61. }
  62. return -1;
  63. }
  64. int f(int64 x) {
  65. if(x <= 10000000) return P[x];
  66. int c = Query(x);
  67. if(c != -1) return c;
  68. int res = 0;
  69. for(int64 i = 2 ; i <= x ; ++i) {
  70. int64 r = x / (x / i);
  71. res = inc(1LL * (r - i + 1) * f(x / i) % MOD,res);
  72. i = r;
  73. }
  74. int64 a = x,b = x + 1;
  75. if(a & 1) b /= 2;
  76. else a /= 2;
  77. a %= MOD;b %= MOD;
  78. res = inc(1LL * a * b % MOD,MOD - res);
  79. Insert(x,res);
  80. return res;
  81. }
  82. int main() {
  83. #ifdef ivorysi
  84. freopen("f1.in","r",stdin);
  85. #endif
  86. phi[1] = 1;P[1] = 1;
  87. for(int i = 2 ; i <= 10000000 ; ++i) {
  88. if(!nonprime[i]) {
  89. prime[++tot] = i;
  90. phi[i] = i - 1;
  91. }
  92. for(int j = 1 ; j <= tot ; ++j) {
  93. if(prime[j] > 10000000 / i) break;
  94. nonprime[prime[j] * i] = 1;
  95. if(i % prime[j] == 0) {phi[i * prime[j]] = phi[i] * prime[j];break;}
  96. else phi[i * prime[j]] = phi[i] * (prime[j] - 1);
  97. }
  98. P[i] = inc(P[i - 1],phi[i]);
  99. }
  100. int64 x;
  101. read(x);
  102. out(f(x));enter;
  103. return 0;
  104. }

【51nod】1239 欧拉函数之和的更多相关文章

  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 1239 欧拉函数之和(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 [题目大意] 计算欧拉函数的前缀和 [题解] 我们 ...

  3. 51nod 1239 欧拉函数之和【欧拉函数+杜教筛】

    和bzoj 3944比较像,但是时间卡的更死 设\( f(n)=\sum_{d|n}\phi(d) g(n)=\sum_{i=1}^{n}f(i) s(n)=\sum_{i=1}^{n}\phi(i) ...

  4. 51 NOD 1239 欧拉函数之和(杜教筛)

    1239 欧拉函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究 ...

  5. 【51nod】1239 欧拉函数之和 杜教筛

    [题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...

  6. 51Nod 1239 欧拉函数前n项和 杜教筛

    http://www.51nod.com/Challenge/Problem.html#!#problemId=1239 AC代码 #include <bits/stdc++.h> #de ...

  7. 51nod1239 欧拉函数之和

    跟1244差不多. //由于(x+1)没有先mod一下一直WA三个点我... //由于(x+1)没有先mod一下一直WA三个点我... #include<cstdio> #include& ...

  8. 欧拉函数之和(51nod 1239)

    对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...

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

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

随机推荐

  1. 洛谷P1230 智力大冲浪

    题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的?!接下来主持人宣布了比赛规则: ...

  2. laravel 5.5 在构造函数使用Session

    public function __construct() { $this->request = request(); // 验证是否登录 $this->middleware(functi ...

  3. 2018南京ICPCMediocre String Problem 马拉车

    hash+二分求出最长公共前缀 然后马拉车+前缀和计数 #include <cstdio> #include <cstring> #include <queue> ...

  4. 通过ida dump Uinity3D的加密dll

    声明: 1.本文转载自:http://www.52pojie.cn/thread-398266-1-1.html,仅供自己保存使用,高手勿喷 2.欢迎学习交流 通过ida dump Uinity3D的 ...

  5. vmware中无法ping通主机的问题

    虚拟机使用NAT方式运行一段时间后,发现无法ping通主机(物理机),显示错误如下 ipconfig如下 查看虚拟机中的网络连接,显示"未识别网络" 分析: 查看了网络上的一些资料 ...

  6. JS中的new操作符原理解析

    var Person = function(name){ this.name = name; } Person.prototype.sayHello = function() { console.lo ...

  7. js控制treeview默认展开

    bootStrapTreeview 在bootstrap的treeview官网,可以找到这个方法,用js控制可以写成:$('#xxx').treeview('collapseNode',{silent ...

  8. 如何在Lunix云服务器上安装Mysql进行远程连接

    说说这个服务器上安装mysql真是一个大坑啊 ! 我也不知道自己怎么心血来潮就买一个百度云服务器,然后就想着吧自己做的一些小项目都跑上去.嘿嘿...其实就是想显摆下,写点小应用给不是编程的朋友们使用 ...

  9. JS-this的用法

    o.onclick=function(){alert(this)}//这个this是指o ------ var arr=[1,2,3,4,5]; arr.a=12; arr.show=function ...

  10. NAIPC2018-K-Zoning Houses

    题目描述 Given a registry of all houses in your state or province, you would like to know the minimum si ...