P4213 [template] Du teach sieve (Sum)

Ideas: Du teach sieve

Submission: \ (2 \) times

Wrong because: \ (\ varphi (i) \) prefix and with \ (int \) stored

answer:

For a multiplicative function sifted prefix and issues du teach sieve time complexity may be lower than a linear problem.
Must first configuration \ (F * G = H \) , and \ (H \) prefix find easy, \ (G \) interval ease requirements.
Specifically:
\ [\ sum_ {I =. 1} ^ {n-} H (I) = \ sum_ {I =. 1} ^ {n-} \ sum_ {D | I} G (D) \ CDOT F (\ FRAC { I} {D}) \] \ [\ sum_ {I =. 1} ^ {n-} H (I) = \ sum_ {D =. 1} ^ {n-} G (D) \ sum_ {I =. 1} ^ { \ lfloor \ frac {n} {
d} \ rfloor} f ({i}) \] provided \ (S (n) \) represents \ (\ sum_ {i = 1 } ^ {n} f (i) \)
\ [\ sum_ {i = 1 } ^ {n} h (i) = \ sum_ {d = 1} ^ {n} g (d) \ cdot S (\ lfloor \ frac {n} {d} \ rfloor) \]
\ [G (. 1) \ CDOT S (n-) = \ sum_ {I =. 1} ^ {n-} H (I) - \ sum_ {D = 2} ^ {n-} G (D) \ CDOT S ( \ lfloor \ frac {n} {
d} \ rfloor) \] when we divisible equation on the back block, seeking \ (S (n) \) complexity is \ (O (n ^ {\ frac {2} {3}}) \)
it is mainly how to construct \ (H \) and \ (G \)
Okay direct say:
\ (\ Epsilon = \ MU \ cdot the I \)
\ (= the above mentioned id \ varphi \ cdot the I \)
to \ (f (n) = \ varphi (n) \ cdot n ^ k = \ varphi (n ^ {k + 1} ) \) is a class of methods:
\ (^ ID +. 1 {K} = F \ CDOT ID ^ K \)

#include<cstdio>
#include<iostream>
#include<unordered_map>
#include<cmath>
#define ll long long
#define R register int
using namespace std;
namespace Luitaryi {
template<class I> inline I g(I& x) { x=0; register I f=1;
  register char ch; while(!isdigit(ch=getchar())) f=ch=='-'?-1:f;
  do x=x*10+(ch^48); while(isdigit(ch=getchar())); return x*=f;
} const int N=5000000,Inf=2147483647;
int T,n,cnt,p[N/4],mu[N+10];
ll phi[N+10];
bool v[N+10];
inline void PRE() { phi[1]=mu[1]=1;
  for(R i=2;i<=N;++i) { 
    if(!v[i]) p[++cnt]=i,phi[i]=i-1,mu[i]=-1;
    for(R j=1;j<=cnt&&i*p[j]<=N;++j) {
      v[i*p[j]]=true;
      if(i%p[j]==0) {
        mu[i*p[j]]=0;
        phi[i*p[j]]=phi[i]*p[j]; break;
      } mu[i*p[j]]=-mu[i];
      phi[i*p[j]]=phi[i]*(p[j]-1);    
    }
  }
  for(R i=1;i<=N;++i) mu[i]+=mu[i-1]; 
  for(R i=1;i<=N;++i) phi[i]+=phi[i-1]; 
}
unordered_map<int,int> memmu;
unordered_map<int,ll> memphi;
inline ll s_phi(int n) {
  if(n<=N) return phi[n];
  if(memphi.count(n)) return memphi[n];
  register ll ans=0;
  for(R l=2,r;r<Inf&&l<=n;l=r+1) {
    r=n/(n/l),ans+=(r-l+1)*s_phi(n/l);
  } return memphi[n]=1llu*n*(n+1ll)/2ll-ans;
}
inline int s_mu(int n) {
  if(n<=N) return mu[n];
  if(memmu.count(n)) return memmu[n];
  register ll ans=0;
  for(R l=2,r;r<Inf&&l<=n;l=r+1) {
    r=n/(n/l),ans+=(r-l+1)*s_mu(n/l);
  } return memmu[n]=1ll-ans;
}
inline void main() { 
  PRE(); g(T); while(T--) {
    g(n); printf("%lld %d\n",s_phi(n),s_mu(n));
  }
}
} signed main() {Luitaryi::main(); return 0;}

2019.08.23
77

Guess you like

Origin www.cnblogs.com/Jackpei/p/11403189.html