BZOJ2226: [Spoj 5971] LCMSum
2015-01-03 12:03
337 查看
题解:
考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和。
这是有公式的f[i]=phi[i]*i/2
然后卡一卡时就可以过了。
代码:
View Code
Submit: 659 Solved: 292
[Submit][Status]
1
2
5
4
55
1 <= T <= 300000
1 <= n <= 1000000
考虑枚举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 MBSubmit: 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
31
2
5
Sample Output
14
55
HINT
Constraints1 <= T <= 300000
1 <= n <= 1000000
相关文章推荐
- [欧拉函数] BZOJ 2226 [Spoj 5971] LCMSum
- [BZOJ]2226: [Spoj 5971] LCMSum 欧拉函数(或莫比乌斯反演)
- BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子
- 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)
- BZOJ 2226 [Spoj 5971] LCMSum 数论
- bzoj 2226: [Spoj 5971] LCMSum (反演)
- [bzoj2226][Spoj 5971] LCMSum
- 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数
- bzoj 2226: [Spoj 5971] LCMSum 数学+欧拉函数
- BZOJ 2226 【SPOJ 5971】 LCMSum
- BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论
- bzoj 2226: [Spoj 5971] LCMSum 数论
- BZOJ 2226 Spoj 5971 LCMSum 数论
- BZOJ 2226: [Spoj 5971] LCMSum( 数论 )
- BZOJ 2226: [Spoj 5971] LCMSum(数论+欧拉函数)
- [BZOJ2226][Spoj 5971] LCMSum(莫比乌斯反演)
- bzoj2226 [Spoj 5971] LCMSum(反演(互质数之和的公式))
- bzoj 2226: [Spoj 5971] LCMSum线性筛欧拉函数
- BZOJ2226: [Spoj 5971] LCMSum
- [BZOJ2226][Spoj5971][数论]LCMSum[好题]