题意:求上面那个函数的值,题目很清楚
题解:欧拉函数的应用,首先我们要把上面那个式子换成我们熟悉能用代码写的复杂度小的形式,因为题目中的条件是除以最小质因子(显然),我们考虑把x换成下面这个形式:
那么我们经过推导很容易得出
然后我们知道欧拉函数的性质(与质数有关的函数考虑欧拉函数):
然后把n换成f(x) :
因为x=f(x)^2而且f(x)是d的倍数(上式可看出)那么x就是d^2的倍数,所以:
所以我们能得到:
这样我们就能写代码了,时间复杂度
上代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 1e7+10;
int pr[MAX],ph[MAX];
bool vis[MAX];
int cnt;
void getph(){//筛欧拉值
ph[1]=1;
vis[0]=vis[1]=true;
for (ll i = 2; i < MAX;i++){
if(!vis[i]){
pr[cnt++]=i;
ph[i]=i-1;
}
for (ll j = 0; j < cnt&&i*pr[j]<MAX;j++){
vis[i*pr[j]]=true;
if(i%pr[j]==0){
ph[i*pr[j]]=ph[i]*pr[j];
break;
}
ph[i*pr[j]]=ph[i]*(pr[j]-1);
}
}
}
int main(){
getph();
int t;
scanf("%d",&t);
while(t--){
ll n;
scanf("%lld",&n);
ll ans=0;
for (ll d = 1; d*d <= n;d++){//注意:用long long
ans+=(n/(d*d))*ph[d];
}
printf("%lld\n",ans);
}
return 0;
}