Description
t 組詢問, 每組詢問給定 n , 求 。
Input
第一行一個整數 t 。
接下來 t 行, 每行一個整數 n 。
Output
輸出共 m 行, 每行一個整數表示答案。
Solution
歐拉函數篩(線性篩求):https://blog.csdn.net/Tc_To_Top/article/details/48025849
我也沒法講得再清楚了,請看學長博客:https://blog.csdn.net/ez_yww/article/details/77922114
只要注意s[1]要取1,因爲但實際上1有貢獻,爲1
%%%yww
Code
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000010;
long long phi[1000010];
long long sum[1000010];
long long s[1000010];
int p[1000010],cnt;
bool vis[1000010];
void work(){
for(int i=2;i<=maxn;i++){
if(!vis[i]){
p[cnt++]=i;
phi[i]=i-1;//線性歐拉篩求phi
}
for(int j=0;j<cnt&&p[j]*i<=maxn;j++){
vis[p[j]*i]=1;
if(i%p[j]==0){
phi[p[j]*i]=p[j]*phi[i];
break;
}
phi[p[j]*i]=phi[p[j]]*phi[i];
}
}
s[1]=1;
for(int i=2;i<=maxn;i++)
s[i]=i*1ll*phi[i]/2;
for(int i=1;i<=maxn;i++)
for(int j=i;j<=maxn;j+=i)
sum[j]+=s[i];
}
int main(){
work();
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("%lld\n",sum[n]*n);
}
}