这题公式真tm难推……为了这题费了我一个草稿本……

woc……在51Nod上码LaTeX码了两个多小时……

一开始码完了前半段,刚码完后半段突然被51Nod吃了,重新码完后半段之后前半段又被吃了,吓得我赶紧换Notepad++接着写……

有的细节懒得再码了,这么一坨LaTeX估计也够你们看了……

\begin{equation}
ans=\sum_{i=1}^n\sum_{j=1}^n [i,j]\\
=2\sum_{i=1}^n\sum_{j=1}^i [i,j]-\frac{n(n+1)}2\\
Let\space s(n)=\sum_{i=1}^n\sum_{j=1}^i [i,j],f(n)=\sum_{i=1}^n [i,n]\\
f(n)=\sum_{i=1}^n [i,n]\\
=\sum_{i=1}^n\frac{in}{(i,n)}\\
=n\sum_{i=1}^n\frac i{(i,n)}\\
=n\sum_{d|n}\sum_{i=1}^n[(i,n)=d]\frac i d\\
=n\sum_{d|n}\sum_{i=1}^{\frac n d}[(i,\frac n d)=1]i\\
=n\sum_{d|n}\sum_{i=1}^{d}[(i,d)=1]i\\
=n\sum_{d|n}\frac{\phi(d)d+[d=1]}2\\
=n\frac{1+\sum_{d|n}\phi(d)d}2\\
s(n)=\sum_{i=1}^n f(i)\\
=\frac{\sum_{i=1}^n i(1+\sum_{d|i}\phi(d)d)}2\\
=\frac{\sum_{i=1}^n i+\sum_{i=1}^n i\sum_{d|i}\phi(d)d}2\\
=\frac{\frac{n(n+1)}2+\sum_{i=1}^n i\sum_{d|i}\phi(d)d}2\\
=\frac{\frac{n(n+1)}2+\sum_{d=1}^n\phi(d)d\sum_{d|i}i}2\\
=\frac{\frac{n(n+1)}2+\sum_{d=1}^n\phi(d)d^2\sum_{i=1}^{\lfloor\frac n d\rfloor}i}2\\
=\frac{\frac{n(n+1)}2+\sum_{i=1}^n i\sum_{d=1}^{\lfloor\frac n i\rfloor}\phi(d)d^2}2\\
ans=2s(n)-\frac{n(n+1)}2\\
=\sum_{i=1}^n i\sum_{d=1}^{\lfloor\frac n i\rfloor}\phi(d)d^2\\
Let \space h(d)=\phi(d)d^2,g(n)=\sum_{d=1}^nh(d)\\
n=\sum_{d|n}\phi(d)\\
n^3=\sum_{d|n}\phi(d)n^2\\
=\sum_{d|n}\phi(d)d^2(\frac n d)^2\\
=\sum_{d|n}h(d)(\frac n d)^2\\
\sum_{i=1}^n i^3=\sum_{i=1}^n\sum_{d|i}h(d)(\frac i d)^2\\
=\sum_{d=1}^n h(d)\sum_{d|i}(\frac i d)^2\\
=\sum_{d=1}^n h(d)\sum_{i=1}^{\lfloor\frac n d \rfloor}i^2\\
=\sum_{i=1}^n i^2\sum_{d=1}^{\lfloor\frac n i\rfloor}h(d)\\
=\sum_{i=1}^n i^2 g(\lfloor\frac n i\rfloor)\\
g(n)=\sum_{i=1}^n i^3-\sum_{i=2}^ni^2 g(\lfloor\frac n i\rfloor)
\end{equation}
然后就是杜教筛的形式了,上杜教筛即可
\begin{equation}
\sum_{i=1}^n i^3=(\frac{n(n+1)}2)^2\\
\sum_{i=1}^n i^2=\frac{n(n+1)(2n+1)}6\\
ans=\sum_{i=1}^n i g(\lfloor\frac n i\rfloor)
\end{equation}
在外面套上一层分块不会影响复杂度,利用g的定义,筛出$\phi$之后即可算出较小的g,较大的g直接杜教筛算即可,总复杂度$O(n^{\frac 2 3})$

贴两份代码(虽然Python2的代码用Python2和Pypy2交都过不去......):

  1. '''
  2. h(i)=phi(d)*d^2
  3. g(i)=sum{h(j)|1<=j<=i}
  4. g(n)=sum{i^3|1<=i<=n}-sum{i^2*g(n/i)|2<=i<=n}
  5. 线筛预处理一部分g,大一些的部分直接上杜教筛即可
  6. s_3(n)=s_1(n)^2,s_2(n)=n(n+1)(2n+1)/6
  7. '''
  8. p=1000000007
  9. table_size=8000000
  10. def get_table(n):
  11. global phi
  12. notp=[False for i in xrange(n+1)]
  13. prime=[]
  14. cnt=0
  15. phi[1]=1
  16. for i in xrange(2,n+1):
  17. if not notp[i]:
  18. prime.append(i)
  19. cnt+=1
  20. phi[i]=i-1
  21. for j in xrange(cnt):
  22. if i*prime[j]>n:
  23. break
  24. notp[i*prime[j]]=True
  25. if i%prime[j]:
  26. phi[i*prime[j]]=phi[i]*(prime[j]-1)
  27. else:
  28. phi[i*prime[j]]=phi[i]*prime[j]
  29. break
  30. for i in xrange(2,n+1):
  31. phi[i]=phi[i]*i*i%p
  32. phi[i]=(phi[i]+phi[i-1])%p
  33. def s1(n):
  34. return (n*(n+1)>>1)%p
  35. def s2(n):
  36. return (n*(n+1)*((n<<1)+1)>>1)/3%p
  37. def S(n):
  38. if n<table_size:
  39. return phi[n]
  40. elif hashmap.has_key(n):
  41. return hashmap[n]
  42. ans=n*(n+1)/2
  43. ans*=ans
  44. ans%=p
  45. i=2
  46. while i<=n:
  47. last=n/(n/i)
  48. #print 'last=%d'%last
  49. ans-=(s2(last)-s2(i-1))*S(n/i)%p
  50. ans%=p
  51. i=last+1
  52. if ans<0:
  53. ans+=p
  54. hashmap[n]=ans
  55. return ans
  56. n=input()
  57. hashmap=dict()
  58. table_size=min(table_size,n)
  59. phi=[0 for i in xrange(table_size+1)]
  60. get_table(table_size)
  61. #print 'table OK'
  62. ans=0
  63. i=1
  64. while i<=n:
  65. last=n/(n/i)
  66. ans+=S(n/i)*(s1(last)-s1(i-1))%p
  67. ans%=p
  68. i=last+1
  69. print ans
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<ext/pb_ds/assoc_container.hpp>
  5. #include<ext/pb_ds/hash_policy.hpp>
  6. #define s1(n) ((long long)(n)%p*(((n)+1)%p)%p*inv_2%p)
  7. #define s2(n) ((long long)(n)%p*(((n)+1)%p)%p*((((long long)(n)%p)<<1)%p+1)%p*inv_6%p)
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. const int table_size=,maxn=table_size+,p=,inv_2=,inv_6=;
  11. void get_table(int);
  12. int S(long long);
  13. bool notp[maxn]={false};
  14. int prime[maxn]={},phi[maxn]={};
  15. gp_hash_table<long long,int>hashmap;
  16. long long n;
  17. int main(){
  18. scanf("%lld",&n);
  19. get_table(min((long long)table_size,n));
  20. int ans=;
  21. for(long long i=,last;i<=n;i=last+){
  22. last=n/(n/i);
  23. ans+=S(n/i)*((s1(last)-s1(i-))%p)%p;
  24. ans%=p;
  25. }
  26. if(ans<)ans+=p;
  27. printf("%d",ans);
  28. return ;
  29. }
  30. void get_table(int n){
  31. phi[]=;
  32. for(int i=;i<=n;i++){
  33. if(!notp[i]){
  34. prime[++prime[]]=i;
  35. phi[i]=i-;
  36. }
  37. for(int j=;j<=prime[]&&i*prime[j]<=n;j++){
  38. notp[i*prime[j]]=true;
  39. if(i%prime[j])phi[i*prime[j]]=phi[i]*(prime[j]-);
  40. else{
  41. phi[i*prime[j]]=phi[i]*prime[j];
  42. break;
  43. }
  44. }
  45. }
  46. for(int i=;i<=n;i++){
  47. phi[i]=(long long)phi[i]*i%p*i%p;
  48. phi[i]=(phi[i]+phi[i-])%p;
  49. }
  50. }
  51. int S(long long n){
  52. if(n<=table_size)return phi[n];
  53. else if(hashmap.find(n)!=hashmap.end())return hashmap[n];
  54. int ans=s1(n)*s1(n)%p;
  55. for(long long i=,last;i<=n;i=last+){
  56. last=n/(n/i);
  57. ans-=S(n/i)*((s2(last)-s2(i-))%p)%p;
  58. ans%=p;
  59. }
  60. if(ans<)ans+=p;
  61. return hashmap[n]=ans;
  62. }
  63. /*
  64. h(i)=phi(d)*d^2
  65. g(i)=sum{h(j)|1<=j<=i}
  66. g(n)=sum{i^3|1<=i<=n}-sum{i^2*g(n/i)|2<=i<=n}
  67. ans=sum{i*g(n/i)|1<=i<=n}
  68. 线筛预处理一部分g,大一些的部分直接上杜教筛即可
  69. s_3(n)=s_1(n)^2,s_2(n)=n(n+1)(2n+1)/6
  70. */

51Nod 最小公倍数之和V3的更多相关文章

  1. 51nod 1238 最小公倍数之和 V3

    51nod 1238 最小公倍数之和 V3 求 \[ \sum_{i=1}^N\sum_{j=1}^N lcm(i,j) \] \(N\leq 10^{10}\) 先按照套路推一波反演的式子: \[ ...

  2. 51NOD 1238 最小公倍数之和 V3 [杜教筛]

    1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...

  3. 【51Nod 1238】最小公倍数之和 V3

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1238 设\(A(n)=\sum\limits_{i=1}^n\frac{ ...

  4. 【51nod】1238 最小公倍数之和 V3 杜教筛

    [题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...

  5. 51nod 1238 最小公倍数之和 V3 【欧拉函数+杜教筛】

    首先题目中给出的代码打错了,少了个等于号,应该是 G=0; for(i=1;i<=N;i++) for(j=1;j<=N;j++) { G = (G + lcm(i,j)) % 10000 ...

  6. 51Nod 1238 最小公倍数之和V3

    题目传送门 分析: 现在我们需要求: \(~~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j)\) \(=\sum_{i=1}^{n}\sum_{j=1}^{n}\frac ...

  7. 51Nod 1238 - 最小公倍数之和 V3(毒瘤数学+杜教筛)

    题目 戳这里 推导 ∑i=1n∑j=1nlcm(i,j)~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j)   ∑i=1n​∑j=1n​lcm(i,j) =∑i=1n∑j= ...

  8. 51 NOD 1238 最小公倍数之和 V3

    原题链接 最近被51NOD的数论题各种刷……(NOI快到了我在干什么啊! 然后发现这题在网上找不到题解……那么既然A了就来骗一波访问量吧…… (然而并不怎么会用什么公式编辑器,写得丑也凑合着看吧…… ...

  9. 51nod1238 最小公倍数之和 V3 莫比乌斯函数 杜教筛

    题意:求\(\sum_{i = 1}^{n}\sum_{j = 1}^{n}lcm(i, j)\). 题解:虽然网上很多题解说用mu卡不过去,,,不过试了一下貌似时间还挺充足的,..也许有时间用phi ...

随机推荐

  1. Scala微服务架构 三

    四 Controller层 之前我们已经把基层架构搭建好了,那么要如何使用呢? 首先看看我的Controller层代码 @Singleton class BMAuthController @Injec ...

  2. 骚年,看我如何把 PhantomJS 图片的 XSS 升级成 SSRF/LFR

    这篇文章实在是太好了,我看了好几篇,所以极力推荐给大家 原文地址   http://buer.haus/2017/06/29/escalating-xss-in-phantomjs-image-ren ...

  3. shell 中的三种引号的作用

    1. 单引号(' ') 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用. 2. 双 ...

  4. python中匿名函数lambda

    简单来说,编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命 名一个函数的场合下使用,也就是指匿名函数. 先看它的几个用法: map( lambda x: x*x, [y f ...

  5. 人生苦短之---第一个Python程序

    第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3​​.x 版本简介 执行 Python 程序的三种方式 解释器 —— python / python ...

  6. Docker部署Vue 工程包

    docker部署 Vue 工程包 目录结构 [root@host ~]# tree front/ front/ ├── dist.conf ├── dist.zip ├── Dockerfile └─ ...

  7. [原创]MOF提权下载者代码

    0x001 网上的mof提权 调用的是js执行添加用户 而且有个缺陷 还不能一步到位...目标3389也连不上...也不知道上面安装了什么软件...毛然添加用户也不好比如有个类似狗之类的拦截添加用户 ...

  8. Spring Boot 2.0正式发布,新特性解读

    作者|翟永超 Spring Boot 2.0 来啦,有哪些新特性?升级吗? 写在前面 北京时间 3 月 1 日,经过漫长的等待之后,Spring Boot 2.0 正式发布.作为 Spring 生态中 ...

  9. (转)tasklist命令参数应用详细图解

    原文:https://blog.csdn.net/bcbobo21cn/article/details/51759521 一 操作实例不带参数: /svc参数: /SVC 显示每个进程中的服务信息,当 ...

  10. postgres 更新数据表

    新增非空列: alter table t_test add column user_id integer; update t_test set user_id=0; alter table t_tes ...