[BZOJ2226]LCMSum
转化一下,$\sum\limits_{i=1}^n[i,n]=n\sum\limits_{i=1}^n\dfrac i{(i,n)}$
枚举$d=(i,n)$,上式变为$n\sum\limits_{d=1}^n\sum\limits_{i=1}^n[(i,n)=d]\dfrac id=n\sum\limits_{d|n}\sum\limits_{i=1}^{\frac nd}\left[\left(i,\dfrac nd\right)=1\right]i$
设$f(n)=\sum\limits_{i=1}^n[(i,n)=1]i$,即互质数和
$$\begin{align*}f(n)&=\sum\limits_{i=1}^ni\sum\limits_{d|(i,n)}\mu(d)\\&=\sum\limits_{d|n}\mu(d)\sum\limits_{\substack{d|i\\i\leq n}}i\\&=\sum\limits_{d|n}d\mu(d)\sum\limits_{i=1}^{\frac nd}i\\&=\dfrac n2\sum\limits_{d|n}\mu(d)\left(\dfrac nd+1\right)\\&=\dfrac n2\left([n=1]+\sum\limits_{d|n}\mu(d)\dfrac nd\right)\\&=\dfrac n2\left([n=1]+\varphi(n)\right)\end{align*}$$
最后一步转变的依据可以用$n=\sum\limits_{d|n}\varphi(d)$反演得到
于是我们可以$O(1)$算$f(n)$了,原式变成$n\sum\limits_{d|n}f\left(\dfrac nd\right)=n\sum\limits_{d|n}f(d)$,$O(\sqrt n)$枚举约数就好了
#include<stdio.h>
#define ll long long
#define T 1000000
int phi[1000010],pr[1000010];
bool np[1000010];
void sieve(){
int i,j,m=0;
np[1]=1;
phi[1]=1;
for(i=2;i<=T;i++){
if(!np[i]){
m++;
pr[m]=i;
phi[i]=i-1;
}
for(j=1;j<=m;j++){
if(pr[j]*(ll)i>T)break;
np[i*pr[j]]=1;
if(i%pr[j]==0){
phi[i*pr[j]]=phi[i]*pr[j];
break;
}else
phi[i*pr[j]]=phi[i]*(pr[j]-1);
}
}
}
ll f(int n){return(phi[n]+(n==1))*(ll)n/2;}
int main(){
sieve();
int t,i,n;
ll s;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
s=0;
for(i=1;i*i<=n;i++){
if(n%i==0){
s+=f(n/i);
if(i*i<n)s+=f(i);
}
}
printf("%lld\n",n*s);
}
}
[BZOJ2226]LCMSum的更多相关文章
- BZOJ2226:LCMSum(欧拉函数)
Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...
- [BZOJ2226][SPOJ5971]LCMSum(莫比乌斯反演)
2226: [Spoj 5971] LCMSum Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1949 Solved: 852[Submit][S ...
- 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)
[BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...
- BZOJ2226: [Spoj 5971] LCMSum
题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...
- BZOJ2226 & SPOJ5971:LCMSum——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题目大意:给定一个n,求lcm(1,n)+lcm(2,n)+……+lcm(n,n). ———— ...
- 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数
题目描述 Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Leas ...
- BZOJ2226:[SPOJ5971]LCMSum
Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...
- [bzoj2226][Spoj5971]LCMSum_欧拉函数_线性筛
LCMSum bzoj-2226 Spoj-5971 题目大意:求$\sum\limits_{i=1}^nlcm(i,n)$ 注释:$1\le n\le 10^6$,$1\le cases \le 3 ...
- spoj LCMSUM sigma(lcm(i,n));
Problem code: LCMSUM Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) ...
随机推荐
- PowerMock
EasyMock 以及 Mockito 都因为可以极大地简化单元测试的书写过程而被许多人应用在自己的工作中,但是这 2 种 Mock 工具都不可以实现对静态函数.构造函数.私有函数.Final 函数以 ...
- [bzoj 2818]欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 枚举最大公约数,对于每一个质数p,只需要求出1<=x,y<=(n/p)范 ...
- bzoj 1201[HNOI2005]数三角形 1202 [HNOI2005]狡猾的商人 暴力 权值并查集
[HNOI2005]数三角形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 349 Solved: 234[Submit][Status][Disc ...
- API教程
www.yuanjiaocheng.net http://www.yuanjiaocheng.net/webapi/test-webapi.html
- bzoj1503 郁闷的出纳员 splay版
自己yy的写法 可能有点奇怪吧 详情看代码 还是蛮短的 #include<cstdio> #include<cstring> #include<algorithm> ...
- 【洛谷 P4168】[Violet]蒲公英(分块)
题目链接 题目大意:给定\(n\)个数和\(m\)个求区间众数的询问,强制在线 这题我\(debug\)了整整一个下午啊..-_- 从14:30~16:45终于\(debug\)出来了,\(debug ...
- NYOJ 349 Sorting It All Out (拓扑排序 )
题目链接 描述 An ascending sorted sequence of distinct values is one in which some form of a less-than ope ...
- [bzoj4765]普通计算姬——分块
Brief Description 给定一棵n个节点的带权树,节点编号为1到n,以root为根,设sum[p]表示以点p为根的这棵子树中所有节点的权 值和.支持下列两种操作: 1 给定两个整数u,v, ...
- win8优化
微软发布win8系统之后受到很多电脑爱好者的关注,都想去尝试体验win8操作系统,对电脑系统优化和系统性能提高一直是我们所追求的.而很多用户都会选择第三方软件对系统进行相关优化,其实我们可以手动对电脑 ...
- python升级3.6后 yum出错File "/usr/bin/yum", line 30 ^
问题描述: # yum provides ifconfig File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ ...