和gcd那题很像。。不过这题组数非常多,需要进一步优化 。。
仍然是把lcm转成gcd,然后枚举gcd
然后可以发现后面其实就是在求n/d内与n/d互质的数的个数(n>2时等于nφ(n)/2),于是
显然符合狄利克雷卷积形式,所以f(n)为积性函数。。而组数那么多,O(n)线性筛应该是最好的选择。。
然后
这个去线性筛的话有点难度,不能直接乘除,因此我们对将n做素因子分解,其中对素因子最小的项,维护
,这样才方便乘除,至于为什么要维护最小的素因子p1,是因为线性筛的过程中每个数只会被他最小的素因子筛到,转移也自然是从最小素因子转移过来,所以只对最小素因子维护就可以了。。。
这样总复杂度为O(n),比网上的方法要优,然而跑起来还是排到了rk50+,bzoj神仙真是多啊。。。orz
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━━━━━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1000005
#define nm 220005
#define N 40005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e16;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,prime[NM],tot;
ll p[NM],f[NM];
bool v[NM];
void init(){
n=1e6;f[1]=1;
inc(i,2,n){
if(!v[i])prime[++tot]=i,p[i]=(ll)i*i*i+1,f[i]=1+(ll)i*(i-1);
inc(j,1,tot){
int t=i*prime[j];
if(t>n)break;
v[t]++;
if(i%prime[j]==0){
p[t]=(p[i]-1)*sqr(prime[j])+1;
f[t]=(prime[j]+1)*f[i]/p[i]*p[t]/(prime[j]+1);
break;
}
p[t]=(ll)prime[j]*prime[j]*prime[j]+1;
f[t]=f[i]*(1+prime[j]*(prime[j]-1));
}
}
inc(i,1,n)f[i]=i*(f[i]+1)/2;
}
int main(){
init();
int _=read();while(_--)printf("%lld\n",f[read()]);
return 0;
}
2226: [Spoj 5971] LCMSum
Time Limit: 20 Sec Memory Limit: 259 MB
Submit: 2080 Solved: 919
[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
Source