BZOJ2226 & SPOJ5971:LCMSum——题解
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——题解的更多相关文章
- [BZOJ2226][SPOJ5971]LCMSum(莫比乌斯反演)
2226: [Spoj 5971] LCMSum Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1949 Solved: 852[Submit][S ...
- 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 ...
- 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题) ...
- 【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】[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:LCMSum(欧拉函数)
Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes t ...
- [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 ...
随机推荐
- 强制删除无用old windows文件夹命令
磁盘上有旧系统留下的目录比如old.windows.program files.users(中文目录是用户,删除命令里还是要用user才有效),因为目录的特殊设置,导致无法直接删除,需要修改属性和权限 ...
- 使用redux-actions优化actions管理
redux-actions的api很少,有三个createAction(s) handleASction(s) combineActions 主要用到createAction去统一管理actio ...
- git配置github链接
1.百度git官网-下载最新版git 2.一路默认下一步安装 3.打开 git bash here 命令行 4.注册github账号(用自己的邮箱就可以,不会英文可以用谷歌翻译)注册成功后建立项目 5 ...
- 【Python+OpenCV】人脸识别基于环境Windows+Python3 version_3(Anaconda3)+OpenCV3.4.3安装配置最新版安装配置教程
注:本次安装因为我要安装的是win10(64bit)python3.7与OpenCV3.4.3教程(当下最新版,记录下时间2018-11-17),实际中这个教程的方法对于win10,32位又或是64位 ...
- [HNOI2017]大佬
参考题解 \(\text{Solution}\) 我们发现5个行为中2操作与其它操作无关,所以我们采用贪心,尽量让多的时间去攻击大佬. 设 \(f[i][j]\) 表示前 \(i\) 天剩 \(j\) ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- 提升方法-AdaBoost
提升方法通过改变训练样本的权重,学习多个分类器(弱分类器/基分类器)并将这些分类器进行线性组合,提高分类的性能. AdaBoost算法的特点是不改变所给的训练数据,而不断改变训练数据权值的分布,使得训 ...
- 五:ResourceManager High Availability RM 高可用
RM有单点失败的风险,但是可以做HA. RMs HA通过master/standby这种结构实现,一个master是active的,其它standby是inactive的.可能通过命令行切换主备节点 ...
- Centos6更新yum repo
163开源镜像站是国内比较老的一个网站.很多人都在使用. step 1/3 备份原镜像文件: cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.r ...
- 一个改变this指向bind的函数,vue源代码
function bind(fn, ctx) { return function (a) { var l = arguments.length; return l ? l > 1 ? fn.ap ...