Euler function + inversion --2019hdu more than 6588 school

\ [Seeking \ sum_ {i = 1} ^ {n} (\ sqrt [3] i, i) \\ at the first conversion equation, consider for i \ in [j ^ 3, (j + 1) ^ 3 -1], \ sqrt [3] i = j \\ it is possible to enumerate all j, then i \ in [j ^ 3, (j + 1) ^ 3-1] interval (i, j) summation we can then n is divided into two parts, the summation: \\ \ sum_ {i = 1} ^ {n} (\ sqrt [3] i, i) = \ sum_ {i = {\ lfloor \ sqrt [3 ] n \ rfloor} ^ 3} ^ {n} (\ sqrt [3] n, n) + \ sum_ {j = 1} ^ {\ sqrt [3] n-1} \ sum_ {i = j ^ 3} ^ {(j + 1) ^ 3-1} (i, j) \\ \]

\ [A front view of the first-formed \ sum_ {i = {\ lfloor \ sqrt [3] n \ rfloor} ^ 3} ^ {n} (\ sqrt [3] n, i), the direct replacement of formula Euler \ \ original formula = \ sum_ {i = {\ lfloor \ sqrt [3] n \ rfloor} ^ 3} ^ {n} \ \ sum_ {d | i, d | {\ lfloor \ sqrt [3] n \ rfloor} } \ varphi (d) = \ sum_ {d | {\ lfloor \ sqrt [3] n \ rfloor}} \ varphi (d) \ big (n / d - (\ lfloor \ sqrt [3] n \ rfloor-1 ) / d \ big) \\ {\ lfloor \ sqrt [3] n \ rfloor} factor is small, only \ sqrt [6] n a, so also the complexity of the \]

\ [Fit a look back, we can be transformed with inversion to quickly after summing \\ \ sum_ {j = 1} ^ {\ sqrt [3] n-1} \ sum_ {i = j ^ 3} ^ {(j + 1) ^ 3-1} (i, j) = \ sum_ {j = 1} ^ {\ sqrt [3] n-1} \ bigg (\ sum_ {i = 1} ^ {(j +1) ^ 3-1} (i, j) - \ sum_ {i = 1} ^ {j ^ 3-1} (i, j) \ bigg) \\ now seek \ sum_ {j = 1} ^ {\ sqrt [3] n-1} \ sum_ {i = 1} ^ {f (j)} (i, j), in fact, it can be (i, j) do expand, but considering the primary Euler Alternatively \\ type = \ sum_ {j = 1} ^ {\ sqrt [3] n-1} \ sum_ {i = 1} ^ {f (j)} \ sum_ {d | i, d | j} \ varphi (d) = \ sum_ {j = 1} ^ {\ sqrt [3] n-1} \ sum_ {d | j} \ varphi (d) \ frac {f (j)} {d} \\ Note that since the upper bound of the i and j is related, so when you switch the order of enumeration can only switch to the front d i, j can not switch to front, otherwise it is impossible to obtain the correct \ varphi (d) the number of occurrences of the original type \\ = \ sum_ {j = 1 } ^ {\ sqrt [3] n-1} \ sum_ {d | j} \ varphi (d) \ bigg (\ lfloor \ frac {(j + 1) ^ 3-1} {d} \ rfloor- \ lfloor \ frac {j ^ 3-1} {d} \ rfloor \ bigg) \ apparently j = kd, it might outer primary enumeration of formula d \\ = \ sum_ {d = 1} ^ {N = \ sqrt [ 3] n-1} \ varphi (d) \ sum_ {k = 1} ^ {\ frac {N} {d}} (3k ^ 2d + 3k + 1) = \ sum_ {d = 1} ^ {N = \ sqrt [3] n-1} \ varphi (d) (3d \ sum_ {k = 1} ^ {\ frac {N} {d}} k ^ 2 + 3 \ sum_ {k = 1} ^ {\ frac {N} {d} } K + \ frac {N} {d}) \\ this point, we may be pretreated, the prefix and the value [1,1e7] range of k ^ 2 k \ varphi (i), and can be O (1 ) is evaluated for each d \]

#include <bits/stdc++.h>
const int N=1e7,XN=N+11,P=998244353;
int Add(int x,int const &y) {return (x+=y)>=P?x-P:x;}
int Minus(int x,int const &y) {return (x-=y)<0?x+P:x;}
int Mul(long long x,int const &y) {return x*y%P;}
int prime[XN],phi[XN],pcnt;
void Prep() {
    static bool notPrime[XN];
    phi[1]=1;
    for(int i=2;i<=N;++i) {
        if(!notPrime[i]) {
            prime[++pcnt]=i;
            phi[i]=i-1;
        }
        for(int j=1;j<=pcnt && i*prime[j]<=N;++j) {
            notPrime[i*prime[j]]=1;
            if(i%prime[j]==0) {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            } else
                phi[i*prime[j]]=phi[i]*phi[prime[j]];
        }
    }
}

int Sum1(long long x) {return x*(x+1)/2%P;}
int Sum2(long long x) {return (x*(x+1))%(6ll*P)*(2*x+1)/6%P;}
int Calc(int a,__int128 L,__int128 R) {
    int res=0;
    for(int d=1;1LL*d*d<=a;++d)
        if(a%d==0) {
            res=Add(res,Mul((R/d-L/d)%P,phi[d]));
            if(a!=d*d)
                res=Add(res,Mul((R/(a/d)-L/(a/d))%P,phi[a/d]));
        }
    return res;
}

int main() {
    Prep();int T;fin>>T;
    while(T--) {
        __int128 n;fin>>n;
        if(n<=7) {fout<<n<<'\n';} 
        else {
            int r;for(r=1;(__int128)(r+2)*(r+2)*(r+2)-1<=n;++r);
            int Ans=Calc(r+1,(__int128)(r+1)*(r+1)*(r+1)-1,n);
            for(int T=1;T<=r;++T)
                Ans=Add(Ans,Mul(phi[T],Add(Add(Mul(3*T,Sum2(r/T)),Mul(3,Sum1(r/T))),r/T)));
            fout<<Ans<<'\n';
        }
    }
}

Guess you like

Origin www.cnblogs.com/zsben991126/p/11365423.html