这题公式真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交都过不去......):

 '''
h(i)=phi(d)*d^2
g(i)=sum{h(j)|1<=j<=i}
g(n)=sum{i^3|1<=i<=n}-sum{i^2*g(n/i)|2<=i<=n}
线筛预处理一部分g,大一些的部分直接上杜教筛即可
s_3(n)=s_1(n)^2,s_2(n)=n(n+1)(2n+1)/6
'''
p=1000000007
table_size=8000000
def get_table(n):
global phi
notp=[False for i in xrange(n+1)]
prime=[]
cnt=0
phi[1]=1
for i in xrange(2,n+1):
if not notp[i]:
prime.append(i)
cnt+=1
phi[i]=i-1
for j in xrange(cnt):
if i*prime[j]>n:
break
notp[i*prime[j]]=True
if i%prime[j]:
phi[i*prime[j]]=phi[i]*(prime[j]-1)
else:
phi[i*prime[j]]=phi[i]*prime[j]
break
for i in xrange(2,n+1):
phi[i]=phi[i]*i*i%p
phi[i]=(phi[i]+phi[i-1])%p
def s1(n):
return (n*(n+1)>>1)%p
def s2(n):
return (n*(n+1)*((n<<1)+1)>>1)/3%p
def S(n):
if n<table_size:
return phi[n]
elif hashmap.has_key(n):
return hashmap[n]
ans=n*(n+1)/2
ans*=ans
ans%=p
i=2
while i<=n:
last=n/(n/i)
#print 'last=%d'%last
ans-=(s2(last)-s2(i-1))*S(n/i)%p
ans%=p
i=last+1
if ans<0:
ans+=p
hashmap[n]=ans
return ans
n=input()
hashmap=dict()
table_size=min(table_size,n)
phi=[0 for i in xrange(table_size+1)]
get_table(table_size)
#print 'table OK'
ans=0
i=1
while i<=n:
last=n/(n/i)
ans+=S(n/i)*(s1(last)-s1(i-1))%p
ans%=p
i=last+1
print ans
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define s1(n) ((long long)(n)%p*(((n)+1)%p)%p*inv_2%p)
#define s2(n) ((long long)(n)%p*(((n)+1)%p)%p*((((long long)(n)%p)<<1)%p+1)%p*inv_6%p)
using namespace std;
using namespace __gnu_pbds;
const int table_size=,maxn=table_size+,p=,inv_2=,inv_6=;
void get_table(int);
int S(long long);
bool notp[maxn]={false};
int prime[maxn]={},phi[maxn]={};
gp_hash_table<long long,int>hashmap;
long long n;
int main(){
scanf("%lld",&n);
get_table(min((long long)table_size,n));
int ans=;
for(long long i=,last;i<=n;i=last+){
last=n/(n/i);
ans+=S(n/i)*((s1(last)-s1(i-))%p)%p;
ans%=p;
}
if(ans<)ans+=p;
printf("%d",ans);
return ;
}
void get_table(int n){
phi[]=;
for(int i=;i<=n;i++){
if(!notp[i]){
prime[++prime[]]=i;
phi[i]=i-;
}
for(int j=;j<=prime[]&&i*prime[j]<=n;j++){
notp[i*prime[j]]=true;
if(i%prime[j])phi[i*prime[j]]=phi[i]*(prime[j]-);
else{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
}
}
for(int i=;i<=n;i++){
phi[i]=(long long)phi[i]*i%p*i%p;
phi[i]=(phi[i]+phi[i-])%p;
}
}
int S(long long n){
if(n<=table_size)return phi[n];
else if(hashmap.find(n)!=hashmap.end())return hashmap[n];
int ans=s1(n)*s1(n)%p;
for(long long i=,last;i<=n;i=last+){
last=n/(n/i);
ans-=S(n/i)*((s2(last)-s2(i-))%p)%p;
ans%=p;
}
if(ans<)ans+=p;
return hashmap[n]=ans;
}
/*
h(i)=phi(d)*d^2
g(i)=sum{h(j)|1<=j<=i}
g(n)=sum{i^3|1<=i<=n}-sum{i^2*g(n/i)|2<=i<=n}
ans=sum{i*g(n/i)|1<=i<=n}
线筛预处理一部分g,大一些的部分直接上杜教筛即可
s_3(n)=s_1(n)^2,s_2(n)=n(n+1)(2n+1)/6
*/

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. Jquery - 添加属性、添加class、添加Css

    一.设置属性: 方式一  jQuery 代码: $("img").attr({ src: "test.jpg", alt: "Test Image&q ...

  2. redis 分布式读写锁

    http://zhangtielei.com/posts/blog-redlock-reasoning.html 链接里这篇 blog 讨论了 redis 分布式锁的实现以及安全性 我要参考 基于单R ...

  3. zoj4110 Strings in the Pocket(manacher)

    传送:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意:给定两个串$S$和$T$,可以翻转$S$串中的任意一个子段, ...

  4. Eclipse 中 Debug 调试 java 代码一直报 Source not found

    今天使用eclipse的debug调试代码,一直没法正常调试,一按F6就提示Source not found 根据提示发现可能是另一个项目影响了,所以把另一个项目Close Project,这次直接t ...

  5. Nginx 负载均衡与反向代理

    通过设置权重来轮询 weight server 192.168.1.62  weight=5 server 192.168.63 weight=1 ip_hash 第3方均衡策略 fair url_h ...

  6. 在IIS建立的ftp,可以成功连接登录,但是不显示目录

    IIS建立FTP站点很简单,不作说明 Windows的防火墙也开通了FTP端口(默认21),Telnet也是通的,在本机可以打开,在局域网其它电脑或外网也可以连接,但就是不显示目录,如果用浏览器打开提 ...

  7. HoloLens开发手记-硬件细节 Hardware Detail

    微软HoloLens是世界第一款完全无线缆的全息计算机.通过在新方式上赋予用户的全息体验,HoloLens重新定义了个人计算(Personal Computing).为了将3D全息图形固定到你周围的真 ...

  8. canvas图片上传相关学习

    今天主要是研究了canvas的关于图片上传的相关知识, context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);  

  9. DevExpress GridControl小结

    1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...

  10. Mac 远程连接 Windows

    推荐使用微软官方发布的 Microsoft Remote Desktop,免费.流畅. 详见:https://docs.microsoft.com/en-us/windows-server/remot ...