http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27840
∑lcm(i,n)=∑i∗n/gcd(i,n)=n∗∑i/gcd(i,n)=n∗∑d|n∑gcd(i,d)=1N/din∗∑d|nd∗φ(d)/2
#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e6+100;
typedef long long LL;
LL phi[N],ans[N];
void init(){
int ed=1000000;
phi[1]=1;
for(int i=2;i<=ed;i++){
if(!phi[i]){
for(int j=i;j<=ed;j+=i){
if(!phi[j])phi[j]=j;
phi[j]=phi[j]-phi[j]/i;
}
}
}
ans[1]=1;
for(int i=2;i<=ed;i++){
ans[i]=i*phi[i]/2+1;
}
for(int i=2;i*i<=ed;i++){
ans[i*i]+=i*phi[i]/2;
for(int j=i+1;j*i<=ed;j++){
ans[i*j]+=i*phi[i]/2+j*phi[j]/2;
}
}
}
int main(){
init();
int T;scanf("%d",&T);
while(T--){
int n;scanf("%d",&n);
printf("%lld\n",ans[n]*n);
}
return 0;
}
本文介绍了一个用于解决数学问题的算法——求解∑lcm(i,n)的高效实现方法。通过数学推导得出核心公式,并利用Euler totient函数优化计算过程。该算法适用于竞赛编程和数论问题求解。
252

被折叠的 条评论
为什么被折叠?



