[bzoj2226][SPOJ5971]LCMSUM
来源:互联网 发布:jquery 数组 json 编辑:程序博客网 时间:2024/05/29 10:42
2226: [Spoj 5971] LCMSum
Time Limit: 20 Sec Memory Limit: 259 MB
Submit: 949 Solved: 427
[Submit][Status][Discuss]
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
首先我们先化简一下式子:
f[i]就表示1~d中与i互质的数的和。怎样去求f[i]呢?
首先如果gcd(x,n)==1,那么gcd(n-x,n)==1
证明:如果gcd(n-x,n)=d(d!=1) 那么n-x=d(n1-x1),那么gcd(x,n)=d>1所以得证。
根据上面的这个性质,我们就可以将与d互质的数分为x和d-x两部分。因因为x+d-x=d,所以
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;const int N=1000010;#define LL long longint n,T,phi[N];inline void prepare(){ int i,j; phi[1]=1; for(i=2;i<=N;++i) if(!phi[i]) for(j=i;j<=N;j+=i){ if(!phi[j]) phi[j]=j; phi[j]=phi[j]/i*(i-1); }}inline void calc(int x){ int i; LL ans=0; for(i=1;i<=sqrt(x);++i) if(!(x%i)){ if(i!=1) ans+=(LL)i*(LL)phi[i]/2; else ans+=1; if(x/i!=i) ans+=(LL)(x/i)*(LL)phi[x/i]/2; } printf("%lld\n",ans*(LL)(x));}int main(){ prepare(); scanf("%d",&T); while(T--){ scanf("%d",&n); calc(n); }}
0 0
- 【bzoj2226】【spoj5971】【lcmsum】【数论】
- [bzoj2226][SPOJ5971]LCMSUM
- [BZOJ2226][Spoj5971][数论]LCMSum[好题]
- bzoj2226 LCMSum
- BZOJ2226: [Spoj 5971] LCMSum
- [bzoj2226] LCMSum 数学+筛法
- BZOJ2226
- spoj LCMSUM
- spoj5971 LCM Sum
- bzoj2226(数论)
- BZOJ2226 基础数论
- SPOJ LCMSUM (数论)
- SPOJ LCMSUM&GCDEX
- SPOJ LCMSUM (数论)
- [BZOJ 2226] LCMSum
- BZOJ 2226 Spoj 5971 LCMSum 数论
- BZOJ 2226 [Spoj 5971] LCMSum 数论
- bzoj2226(又是智商的突破,加油)
- IndexedDB: 浏览器里内置的数据库简介
- ENVI遥感影像自然真彩色增强方法
- 浅析c++中的类型转换--const_cast
- 算法(一)求链表中的倒数第K 个结点
- [Win32] ShellHook的实现
- [bzoj2226][SPOJ5971]LCMSUM
- Linux鸟哥私房菜学习笔记(四)
- 进程&线程小结
- Python编写简易木马程序
- Oracle 简单操作手册
- 4-10 阶乘计算升级版 (20分)
- Spring实现AOP的4种方式
- JAVA开发思想与实例--模块化编程
- NDK started