您的位置:首页 > 其它

BZOJ2226: [Spoj 5971] LCMSum

2015-01-03 12:03 337 查看
题解:

考虑枚举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 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=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
return x*f;
}
int tot,p[maxn];
ll fai[maxn],ans[maxn];
bool v[maxn];
void get()
{
fai[1]=1;
for2(i,2,maxm)
{
if(!v[i])p[++tot]=i,fai[i]=i-1;
for1(j,tot)
{
int k=i*p[j];
if(k>maxm)break;
v[k]=1;
if(i%p[j])fai[k]=fai[i]*(p[j]-1);
else {fai[k]=fai[i]*p[j];break;}
}
}
for2(i,3,maxm)(fai[i]*=(ll)i)>>=1;
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 0;
}


View Code

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: