本地 10s 过,Bzoj 20s 都跑不下来......
Code:
#include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) #define maxn 1000006 #define M 1000004 #define Sum(n) (1ll*(1ll*n*1ll*(n+1))/2) #define ll long long #define O2 __attribute__((optimize("-O2"))) using namespace std; char *p1,*p2,buf[100000]; #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++) int rd() {int x=0; char c=nc(); while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x;} int cnt,edges; bool vis[maxn]; int mu[maxn],prime[maxn],hd[maxn],to[maxn*20],nex[maxn*20]; ll sumv[maxn]; O2 inline void Linear_shaker() { int i,j; mu[1]=1; for(i=1;i<=M;++i) for(j=i;j<=M;j+=i) nex[++edges]=hd[j],hd[j]=edges,to[edges]=i; for(i=2;i<=M;++i) { if(!vis[i]) mu[i]=-1,prime[++cnt]=i; for(j=1;j<=cnt&&1ll*i*prime[j]<=M;++j) { vis[i*prime[j]]=1; if(i%prime[j]==0) { mu[i*prime[j]]=0; break; } mu[i*prime[j]]=-mu[i]; } } for(i=1;i<=M;++i) for(j=hd[i];j;j=nex[j]) sumv[i]+=mu[to[j]]*to[j]; } O2 int main() { // setIO("input"); Linear_shaker(); int T=rd(); while(T--) { int n,i; n=rd(); ll re=0; for(i=hd[n];i;i=nex[i]) re+=sumv[to[i]]*Sum(n/to[i]); re*=n; printf("%lld\n",re); } return 0; }