版权声明:蒟蒻Blog随意转载 https://blog.csdn.net/a1799342217/article/details/82390344
欧拉函数
来推柿子:
预处理 的前缀和,然后除法分块一波就没了。
可以把前面 换成其他函数。
不懂第二步到第三步的同学戳这里,很简单的一个结论。
代码:
#include<cctype>
#include<cstdio>
#include<algorithm>
#define F inline
using namespace std;
typedef long long LL;
const int N=1e7+5;
int t,n,p[N]; LL phi[N],ans[5005];
bool f[N];
F char readc(){
static char buf[100000],*l=buf,*r=buf;
if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
return l==r?EOF:*l++;
}
F int _read(){
int x=0; char ch=readc();
while (!isdigit(ch)) ch=readc();
while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
return x;
}
F void Make(int n){
phi[1]=1;
for (int i=2;i<=n;i++){
if (!f[i]) p[++p[0]]=i,phi[i]=i-1;
for (int j=1,v;j<=p[0]&&i*p[j]<=n;j++){
f[v=i*p[j]]=true;
if (i%p[j]==0){ phi[v]=phi[i]*p[j]; break; }
phi[v]=phi[i]*phi[p[j]];
}
}
for (int i=2;i<=n;i++) phi[i]+=phi[i-1];
}
F LL calc(int n){
LL ans=0;
for (LL l=1,r;l<=n;l=r+1)
r=n/(n/l),ans+=(phi[r]-phi[l-1])*((phi[n/l]<<1)-1);
return ans;
}
#define max(x,y) ((x)>(y)?(x):(y))
int main(){
t=_read(); int mx=0,tmp=t;
for (int i=1;i<=t;i++)
ans[i]=_read(),mx=max(mx,ans[i]);
for (Make(mx);t;t--) ans[t]=calc(ans[t]);
for (int i=1;i<=tmp;i++) printf("%lld\n",ans[i]);
return 0;
}