BZOJ.3944.Sum(Min_25筛)

BZOJ
洛谷

不得不再次吐槽洛谷数据好水(连\(n=0,2^{31}-1\)都没有)。

\(Description\)

给定\(n\),分别求\[\sum_{i=1}^n\varphi(i),\quad\sum_{i=1}^n\mu(i)\]

\(n\lt2^{31}\)

\(Solution\)

\(\varphi(p)=p-1,\quad\mu(p)=-1\)
\(g(i)\)表示\(1\sim i\)的质数和,\(h(i)\)表示\(1\sim i\)的质数个数,那么\(\varphi(i)\)的前缀和就用\(g(i)-h(i)\)求,\(\mu(i)\)的前缀和就用\(-h(i)\)求。

\(\varphi(p^k)=p^{k-1}(p-1)\)
发现自己不知道这个。。

Min_25比杜教筛不知道快了多少。

//3128kb    4632ms
#include <cmath>
#include <cstdio>
#include <algorithm>
typedef long long LL;
const int N=47000<<1;

int n,m,Sqr,cnt,P[N>>2],h[N],w[N];
LL sp[N],g[N];

inline int ID(int x)
{
    return x<=Sqr?x:m-n/x+1;
}
LL S_Phi(int x,int y)
{
    if(x<=1||P[y]>x) return 0;
    LL res=g[ID(x)]-sp[y-1]+y-1;
    for(int i=y; i<=cnt&&P[i]*P[i]<=x; ++i)//p*p当然<=n 
        for(LL p=P[i],p1=p,t=p-1; 1ll*p1*p<=x/*这里会爆int!*/; p1*=p,t*=p)
            res+=1ll*(S_Phi(x/p1,i+1)+p)*t;//x/p1不是n/p1!
    return res;
}
int S_Mu(int x,int y)
{
    if(x<=1||P[y]>x) return 0;
    int res=h[ID(x)]+y-1;//-h!
    for(int i=y; i<=cnt&&P[i]*P[i]<=x; ++i)
        res-=S_Mu(x/P[i],i+1);
    return res;
}
void Solve()
{
    scanf("%d",&n), Sqr=sqrt(n);
    if(!n) return (void)puts("0 0");
    cnt=m=0;
    for(LL i=1; i<=n; i=w[m]+1ll)
        w[++m]=n/(n/i), g[m]=(1ll*w[m]*((LL)w[m]+1)>>1)-1, h[m]=w[m]-1;//(LL)w[m]+1!!!
    for(int j=2; j<=Sqr; ++j)
        if(g[j]!=g[j-1])
        {
            P[++cnt]=j, sp[cnt]=sp[cnt-1]+j; int lim=j*j;
            for(int i=m; lim<=w[i]; --i)
            {
                int k=ID(w[i]/j);
                g[i]-=1ll*j*(g[k]-sp[cnt-1]);//不取模。。
                h[i]-=h[k]-cnt+1;
            }
        }
    for(int i=1; i<=m; ++i) g[i]-=h[i], h[i]=-h[i];
    printf("%lld %d\n",S_Phi(n,1)+1,S_Mu(n,1)+1);//f(1)!
}

int main()
{
    int T;
    for(scanf("%d",&T); T--; Solve());
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/SovietPower/p/10104516.html