http://www.lydsy.com/JudgeOnline/problem.php?id=2226

题目大意:给定一个n,求lcm(1,n)+lcm(2,n)+……+lcm(n,n)。

——————————————————————————————

如果是刚做完[SDOI2012]Longge的问题的话这道题应该能轻松一些。

显然答案可以转化为∑n*i/gcd(n,i)。

设k=gcd(n,i),则可以转化为∑n*i/k(k|n且gcd(n,i)=k),然后变成n*(∑i/k)(k|n且gcd(n,i)=k)。

又因为gcd(n/k,i/k)=1,所以对于一个k,∑i/k就是与n/k互质的数的和。

而对于一个数n,求与n互质的数的和=n*phi(n)/2。

于是这题我们就做完了。

PS:秉承着万恶的SPOJ题的尿性,这题有点卡常数。

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=;
ll phi[N],su[N];
bool he[N];
void Euler(int n){
int tot=;
phi[]=;
for(int i=;i<=n;i++){
if(!he[i]){
su[++tot]=i;
phi[i]=i-;
}
for(int j=;j<=tot;j++){
if(i*su[j]>=n)break;
he[i*su[j]]=;
if(i%su[j]==){
phi[i*su[j]]=phi[i]*su[j];break;
}
else phi[i*su[j]]=phi[i]*(su[j]-);
}
}
return;
}
int main(){
Euler();
int t;
scanf("%d",&t);
while(t--){
int n;ll ans=;
scanf("%d",&n);
for(int i=;i*i<=n;i++){
if(n%i)continue;
int k=n/i;
ans+=(ll)(phi[k]*k+)>>;
if(i*i<n)ans+=(ll)(phi[i]*i+)>>;
}
printf("%lld\n",ans*n);
}
return ;
}

BZOJ2226 & SPOJ5971:LCMSum——题解的更多相关文章

  1. [BZOJ2226][SPOJ5971]LCMSum(莫比乌斯反演)

    2226: [Spoj 5971] LCMSum Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1949  Solved: 852[Submit][S ...

  2. BZOJ2226:[SPOJ5971]LCMSum

    Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...

  3. [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 ...

  4. AHOI2018训练日程(3.10~4.12)

    (总计:共90题) 3.10~3.16:17题 3.17~3.23:6题 3.24~3.30:17题 3.31~4.6:21题 4.7~4.12:29题 ZJOI&&FJOI(6题) ...

  5. 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)

    [BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...

  6. BZOJ2226: [Spoj 5971] LCMSum

    题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...

  7. 【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 ...

  8. BZOJ2226:LCMSum(欧拉函数)

    Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...

  9. [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. PHP程序员如何理解依赖注入容器(dependency injection container)

    背景知识 传统的思路是应用程序用到一个Foo类,就会创建Foo类并调用Foo类的方法,假如这个方法内需要一个Bar类,就会创建Bar类并调用Bar类的方法,而这个方法内需要一个Bim类,就会创建Bim ...

  2. uvaoj 101 - The Blocks Problem(vector应用+技巧)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page= ...

  3. uvaoj 10474 - Where is the Marble?(sort+lower_bound)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. 第一模块·开发基础-第1章 Python基础语法

    Python开发工具课前预习 01 Python全栈开发课程介绍1 02 Python全栈开发课程介绍2 03 Python全栈开发课程介绍3 04 编程语言介绍(一) 05 编程语言介绍(二)机器语 ...

  5. 解决ssh_exchange_identification:read connection reset by peer 原因

    服务器改了密码,试过密码多次后出现: ssh_exchange_identification: read: Connection reset by peer 可以通过ssh -v查看连接时详情 Ope ...

  6. DNA序列 (DNA Consensus String,ACM/ICPC Seoul 2006,UVa1368

    题目描述:算法竞赛入门经典习题3-7 题目思路:每列出现最多的距离即最短 #include <stdio.h> #include <string.h> int main(int ...

  7. Window下部署MySql数据库

    官网下载地址:https://dev.mysql.com/downloads/mysql/,MySQL Community(社区版) Server 5.7.21,下载完毕后,解压文件. (1)在mys ...

  8. [C++ map & dp]codeforces 960F. Pathwalks

    题目传送门:960F 思路: 题目给人的感觉很像最长上升子序列,自然而然想到用dp的思路去处理 题目中给的限制条件是,要接上前面的边,前面的边权一定要小于当前的边权(题目按照输入的顺序,因此只找前面的 ...

  9. 【Linux】Face Recognition的封装

    使用虹软的人脸识别 写了一个linux下的Face Recognition的封装,当作是练习. C++的封装,结合opencv,使用方便.https://github.com/zacario-li/F ...

  10. str和repr

    在Python2.6和Python3.0以及更早的版本中,在交互式模式下的输出本质上是使用repr,因此对于一些浮点数运算,会显示很多位: 4 / 5.0 #0.8000000000000004 但是 ...