题解:

考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和。

这是有公式的f[i]=phi[i]*i/2

然后卡一卡时就可以过了。

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 1000000+5
#define maxm 100000+5
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int tot,p[maxn];
ll fai[maxn];
bool v[maxn];
void get()
{
fai[]=;
for2(i,,)
{
if(!v[i])p[++tot]=i,fai[i]=i-;
for1(j,tot)
{
int k=i*p[j];
if(k>)break;
v[k]=;
if(i%p[j])fai[k]=fai[i]*(p[j]-);
else {fai[k]=fai[i]*p[j];break;}
}
}
for2(i,,)(fai[i]*=(ll)i)>>=;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
get();
int T=read();
while(T--)
{
int n=read(),m=sqrt(n);ll ans=;
for1(i,m)if(n%i==)ans+=fai[n/i]+fai[i];
if(m*m==n)ans-=fai[m];
printf("%lld\n",ans*(ll)n);
}
return ;
}

UPD:其实我们可以预处理出答案,用普通的筛法。

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 1000000+5
#define maxm 1000000
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int tot,p[maxn];
ll fai[maxn],ans[maxn];
bool v[maxn];
void get()
{
fai[]=;
for2(i,,maxm)
{
if(!v[i])p[++tot]=i,fai[i]=i-;
for1(j,tot)
{
int k=i*p[j];
if(k>maxm)break;
v[k]=;
if(i%p[j])fai[k]=fai[i]*(p[j]-);
else {fai[k]=fai[i]*p[j];break;}
}
}
for2(i,,maxm)(fai[i]*=(ll)i)>>=;
for1(i,maxm)
for(int j=i;j<=maxm;j+=i)
ans[j]+=fai[i];
for1(i,maxm)ans[i]*=(ll)i;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
get();
int T=read();
while(T--)printf("%lld\n",ans[read()]);
return ;
}

2226: [Spoj 5971] LCMSum

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 659  Solved: 292
[Submit][Status]

Description

Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n.

Input

The first line contains T the number of test cases. Each of the next T lines contain an integer n.

Output

Output T lines, one for each test case, containing the required sum.

Sample Input

3
1
2
5

Sample Output

1
4
55

HINT

Constraints

1 <= T <= 300000
1 <= n <= 1000000

BZOJ2226: [Spoj 5971] LCMSum的更多相关文章

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

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

  2. bzoj 2226: [Spoj 5971] LCMSum 数论

    2226: [Spoj 5971] LCMSum Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 578  Solved: 259[Submit][St ...

  3. BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论

    BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...

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

  5. BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题解: 题目要求的是Σn*i/gcd(i,n) i∈[1,n] 把n提出来变成Σi/g ...

  6. BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常

    Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...

  7. BZOJ 2226 [Spoj 5971] LCMSum

    题解:枚举gcd,算每个gcd对答案的贡献,贡献用到欧拉函数的一个结论 最后用nlogn预处理一下,O(1)出答案 把long long 打成int 竟然没看出来QWQ #include<ios ...

  8. BZOJ 2226 【SPOJ 5971】 LCMSum

    题目链接:LCMSum 这个题显然就是要我们推式子了……那么就来推一波: \begin{aligned}&\sum_{i=1}^n lcm(i,n) \\=&\sum_{i=1}^n\ ...

  9. 【spoj 5971】lcmsum

    全场都 AK 了就我爆 0 了 题意 \(t\) 组询问,每组询问给定 \(n\),求 \(\sum\limits_{k=1}^n [n,k]\).其中 \([a,b]\) 表示 \(a\) 和 \( ...

随机推荐

  1. [转载+原创]Emgu CV on C# (二) —— Emgu CV on 灰度化

    本文主要对彩色图片灰度化的方法及其实现过程进行总结,最终给出Emgu CV实现的代码. 一.灰度化原理及数学实现(转载自——<图像灰度化方法总结及其VC实现> 该篇文章使用opencv实现 ...

  2. 将Asp.Net MVC应用程序的控制器定义在单独的程序集(类库)中

    一直以来都想把控制器的代码部署到单独的程序集里.昨天研究Asp.Net MVC的源代码,偶然发现有一个奇特的类“ControllerBuilder”,MSDN上的介绍相当简略,就一句话“表示一个类,该 ...

  3. FTP操作类(支持异步)

    public delegate void DownloadProgressChangedEventHandle(string information, long currentprogress, lo ...

  4. oracle 建立主键与索引【转】

    此文转自:http://blog.sina.com.cn/s/blog_439f80c4010094n1.html 创建主键: alter table T add primary key (V) T是 ...

  5. PHP读取xml之cdata讲解

    实例: xss.xml <?xml version="1.0" encoding="UTF-8"?><filters>    <f ...

  6. uva 307

    排序之后再剪枝,有点神 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...

  7. Unity3d Detect NetState

    public static bool HasConnection() { System.Net.WebClient client; System.IO.Stream stream; try { usi ...

  8. mysql导出多个表数据为excel方法,substring函数查询

    //查询sys_username以S.00655开头的用户 ),sys_password FROM `tbl_sa_syslogin` where sys_username like 'S.%'; / ...

  9. OneAPM 技术公开课第二讲:开启性能为王的架构时代

    「OneAPM 技术公开课」由应用性能管理第一品牌 OneAPM 发起,内容面向 IT 开发和运维人员.云集技术牛人.知名架构师.实践专家共同探讨技术热点.继北京站第一场火爆上演之后,第二场将于9月1 ...

  10. CentOS下安装Redmine 2.5.2

    Redmine是用Ruby开发的基于web的项目管理软件,所以先要下载安装Ruby,再下载对 Ruby组件进行打包的 Ruby 打包系统RubyGems. 第一步:下载必要的软件 1.依赖包安装 # ...