正题
题目链接:
https://www.lydsy.com/JudgeOnline/problem.php?id=2226
https://www.luogu.org/problem/SP5971
题目大意
求∑i=1nlcm(n,i)\sum_{i=1}^n lcm(n,i)i=1∑nlcm(n,i)
解题思路
∑i=1nlcm(n,i)\sum_{i=1}^n lcm(n,i)i=1∑nlcm(n,i)
∑i=1nnigcd(n,i)\sum_{i=1}^n \frac{ni}{gcd(n,i)}i=1∑ngcd(n,i)ni
n∑d∣n∑i=1n[(n,i)==d]in\sum_{d\mid n}\sum_{i=1}^n[(n,i)==d]ind∣n∑i=1∑n[(n,i)==d]i
n∑d∣n∑i=1n[(n/d,i/d)==1]in\sum_{d\mid n}\sum_{i=1}^n[(n/d,i/d)==1]ind∣n∑i=1∑n[(n/d,i/d)==1]i
n∑d∣nd∗φ(d)2n\sum_{d|n}\frac{d*\varphi(d)}{2}nd∣n∑2d∗φ(d)
然后预处理O(nlogn)O(n\log n)O(nlogn)然后O(1)O(1)O(1)回答即可
时间复杂度O(nlogn+T)O(n\log n+T)O(nlogn+T)
codecodecode
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const ll N=1e6;
ll T,phi[N+10],ans[N+10],n;
int main()
{for (ll i=2;i<=N;i++) phi[i]=i;for (ll i=2;i<=N;i++){bool flag=(phi[i]==i);for (ll j=i;j<=N;j+=i){if(flag)phi[j]=phi[j]/i*(i-1);ans[j]+=phi[i]*i/2;}}scanf("%lld",&T);while(T--){scanf("%lld",&n);printf("%lld\n",n*(ans[n]+1));}
}