51Nod 最小公倍数之和V3
这题公式真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的更多相关文章
- 51nod 1238 最小公倍数之和 V3
51nod 1238 最小公倍数之和 V3 求 \[ \sum_{i=1}^N\sum_{j=1}^N lcm(i,j) \] \(N\leq 10^{10}\) 先按照套路推一波反演的式子: \[ ...
- 51NOD 1238 最小公倍数之和 V3 [杜教筛]
1238 最小公倍数之和 V3 三种做法!!! 见学习笔记,这里只贴代码 #include <iostream> #include <cstdio> #include < ...
- 【51Nod 1238】最小公倍数之和 V3
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1238 设\(A(n)=\sum\limits_{i=1}^n\frac{ ...
- 【51nod】1238 最小公倍数之和 V3 杜教筛
[题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...
- 51nod 1238 最小公倍数之和 V3 【欧拉函数+杜教筛】
首先题目中给出的代码打错了,少了个等于号,应该是 G=0; for(i=1;i<=N;i++) for(j=1;j<=N;j++) { G = (G + lcm(i,j)) % 10000 ...
- 51Nod 1238 最小公倍数之和V3
题目传送门 分析: 现在我们需要求: \(~~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j)\) \(=\sum_{i=1}^{n}\sum_{j=1}^{n}\frac ...
- 51Nod 1238 - 最小公倍数之和 V3(毒瘤数学+杜教筛)
题目 戳这里 推导 ∑i=1n∑j=1nlcm(i,j)~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j) ∑i=1n∑j=1nlcm(i,j) =∑i=1n∑j= ...
- 51 NOD 1238 最小公倍数之和 V3
原题链接 最近被51NOD的数论题各种刷……(NOI快到了我在干什么啊! 然后发现这题在网上找不到题解……那么既然A了就来骗一波访问量吧…… (然而并不怎么会用什么公式编辑器,写得丑也凑合着看吧…… ...
- 51nod1238 最小公倍数之和 V3 莫比乌斯函数 杜教筛
题意:求\(\sum_{i = 1}^{n}\sum_{j = 1}^{n}lcm(i, j)\). 题解:虽然网上很多题解说用mu卡不过去,,,不过试了一下貌似时间还挺充足的,..也许有时间用phi ...
随机推荐
- Scala微服务架构 三
四 Controller层 之前我们已经把基层架构搭建好了,那么要如何使用呢? 首先看看我的Controller层代码 @Singleton class BMAuthController @Injec ...
- 骚年,看我如何把 PhantomJS 图片的 XSS 升级成 SSRF/LFR
这篇文章实在是太好了,我看了好几篇,所以极力推荐给大家 原文地址 http://buer.haus/2017/06/29/escalating-xss-in-phantomjs-image-ren ...
- shell 中的三种引号的作用
1. 单引号(' ') 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用. 2. 双 ...
- python中匿名函数lambda
简单来说,编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命 名一个函数的场合下使用,也就是指匿名函数. 先看它的几个用法: map( lambda x: x*x, [y f ...
- 人生苦短之---第一个Python程序
第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3.x 版本简介 执行 Python 程序的三种方式 解释器 —— python / python ...
- Docker部署Vue 工程包
docker部署 Vue 工程包 目录结构 [root@host ~]# tree front/ front/ ├── dist.conf ├── dist.zip ├── Dockerfile └─ ...
- [原创]MOF提权下载者代码
0x001 网上的mof提权 调用的是js执行添加用户 而且有个缺陷 还不能一步到位...目标3389也连不上...也不知道上面安装了什么软件...毛然添加用户也不好比如有个类似狗之类的拦截添加用户 ...
- Spring Boot 2.0正式发布,新特性解读
作者|翟永超 Spring Boot 2.0 来啦,有哪些新特性?升级吗? 写在前面 北京时间 3 月 1 日,经过漫长的等待之后,Spring Boot 2.0 正式发布.作为 Spring 生态中 ...
- (转)tasklist命令参数应用详细图解
原文:https://blog.csdn.net/bcbobo21cn/article/details/51759521 一 操作实例不带参数: /svc参数: /SVC 显示每个进程中的服务信息,当 ...
- postgres 更新数据表
新增非空列: alter table t_test add column user_id integer; update t_test set user_id=0; alter table t_tes ...