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. 利用JSON Schema校验JSON数据格式

    最近笔者在工作中需要监控一批http接口,并对返回的JSON数据进行校验.正好之前在某前端大神的分享中得知这个神器的存在,调研一番之后应用在该项目中,并取得了不错的效果,特地在此分享给各位读者. 什么 ...

  2. 各种对list,string操作函数的总结

    #encoding=utf-8#reverse,用来反转lista=['aa','bb','cc']a.reverse()print a#['cc', 'bb', 'aa']#不能直接print a. ...

  3. [wirtting] top01 independent

    Do you agree or disagree with the following statement? At universities and colleges, sports and soci ...

  4. Unity编辑器 - 资源批处理工具基类

    Unity编辑器 - 资源批处理工具基类 经常要对资源进行批处理,很多时候都是一次性的需求,于是弄个通用脚本. 工具是个弹出面板,处理过程有进度条,如下: 如图,子类只需要重写几个方法: using ...

  5. C#冒泡排序法及优化

    冒泡排序法及优化: static void Main(string[] args) { , , , , , }; ; //冒泡排序法 ; i < sums.Length - ; i++) //总 ...

  6. CSP201403-2:窗口

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  7. 352[LeetCode] Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. Python3 异常与断言

    1.异常 当出现错误时,程序就会发生异常 num1=input('Please input a num1: ') num2=input('Please input a num2: ') print(f ...

  9. chameleon-Mini(迷你变色龙)

    Chameleon Mini(迷你变色龙)是一个比一般信用卡稍大的小型开发板,是开源产品. 如图 Chameleon Mini可以完全复制许多商业非接触式智能卡包括UID卡,在内的全部内容,因此可以用 ...

  10. Eclipse安装颜色主题,个性化你的IDE,让你的IDE焕然一新

    我们都知道eclipse默认的颜色主题是白色的背景,但是如果想改变代码编辑区的背景颜色,需要怎么办呢? 今天给大家介绍一个非常赞的eclipse,可以很方便的根据自己的需求选择喜欢的颜色主题,其他的不 ...