[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) ...
随机推荐
- 理解[].forEach.call()
例子: let cols = document.querySelectorAll('ul li') [].forEach.call(cols, function (col, index) { // T ...
- Codeforces Round #328 (Div. 2) A
A. PawnChess time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- n元线性方程非负整数解的个数问题
设方程x1+x2+x3+...+xn = m(m是常数) 这个方程的非负整数解的个数有(m+n-1)!/((n-1)!m!),也就是C(n+m-1,m). 具体解释就是m个1和n-1个0做重集的全排列 ...
- C. Annoying Present SB题
C. Annoying Present time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- spring 中的@Import注解和@ImportResource注解
概述:@Import注解是引入带有@Configuration的java类. @ImportResource是引入spring配置文件.xml 案例的核心代码如下: package com.timo. ...
- BS架构下使用消息队列的工作流程
异步通信 对于BS(Browser-Server 浏览器)架构,很多情景下server的处理时间较长. 如果浏览器发送请求后,保持跟server的连接,等待server响应,那么一方面会对用户的体验有 ...
- hadoop 架构
- IOS 上传项目到github 终端操作
1.创建github账号 2.创建秘钥 3.Github配置秘钥 4.上传文件 复制保存网址 终端操作,如果没有ssh,自行安装 GitHub配置秘钥 克隆github上创建的项目 将自己的本地项目, ...
- Java并发(6)- CountDownLatch、Semaphore与AQS
引言 上一篇文章中详细分析了基于AQS的ReentrantLock原理,ReentrantLock通过AQS中的state变量0和1之间的转换代表了独占锁.那么可以思考一下,当state变量大于1时代 ...
- Lucene4.6查询时完全跳过打分,提高查询效率的实现方式
由于索引的文件量比较大,而且应用中不需要对文档进行打分,只需要查询出所有满足条件的文档.所以需要跳过打分来提高查询效率.一开始想用ConstantScoreQuery,但是测试发现这个类虽然让所有返回 ...