[bzoj4804]欧拉心算

Orz w_yqts
裸的反演..
http://blog.csdn.net/w_yqts/article/details/79025198

#include 
using namespace std;
#define ll long long
#define N 10000001
ll f[N];
bool flag[N];
int pn,pr[N];
void init()
{
    flag[1]=1;f[1]=1LL;
    for (int i=2;iif (!flag[i]) pr[++pn]=i,f[i]=(i-1)+(-1);
        for (int j=1;j<=pn && pr[j]*i*pr[j]]=1;
            if (i%pr[j]==0)
            {
                if ((i/pr[j])%pr[j]==0) f[i*pr[j]]=f[i]*pr[j];
                else f[i*pr[j]]=f[i/pr[j]]*(pr[j]-1)*(pr[j]-1);
                break;
            }
            f[i*pr[j]]=f[i]*f[pr[j]];
        }
    }
    for (int i=2;i1];
}
int n;
inline void solve()
{
    ll ans=0LL;
    scanf("%d",&n);
    for (int i=1,pos;i<=n;i=pos+1)
    {
        pos=n/(n/i);
        ans+=(f[pos]-f[i-1])*(n/i)*(n/i);
    }
    printf("%lld\n",ans);
}
int main()
{
    init();
    int T;
    cin>>T;
    while (T--) solve();
}

你可能感兴趣的:(莫比乌斯反演)