BZOJ3739: DZY loves math VIII

莫比乌斯反演
可以推出来
不会Markdown。。。。

http://www.cnblogs.com/clrs97/p/5063707.html

#include<cstdio>
#include<iostream>

#define ll long long
char c;
inline void read(ll&a)
{
    a=0;do c=getchar();while(c<'0'||c>'9');
    while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();
}
const int Maxn=10000001; 
int Prime[Maxn],tot;
ll S[Maxn];
int Mu[Maxn];
int Factor[Maxn];
bool Check[Maxn];
ll B,A;
ll ans; 
int s[Maxn];
int Cache[101];
inline void Search(int Pos,int Cur)
{
    if(Pos^(tot+1))Search(Pos+1,Cur),Search(Pos+1,Cur*Cache[Pos]);
    else
         s[Cur]+=A,B+=Mu[Cur]*s[Cur];
}

int main()
{ 
  ll k;
  ll T,n,m,i,j;
  Mu[1]=1;
  Factor[1]=1;
  for(i=2;i<Maxn;i++)
   { 
    if(!Check[i])
     Factor[i]=Prime[++tot]=i,Mu[i]=-1; 
    for(j=1;j<=tot;j++)
    { 
      k=i*Prime[j];
      if(k>=Maxn)break; 
      Check[k]=1; 
      Factor[k]=Prime[j];
      if(i%Prime[j])Mu[k]=-Mu[i];  
      else
       {Mu[k]=0;break; } 
    } 
  } 
  //for(i=2;i<Maxn;i++)Mu[i]+=Mu[i-1]; 
  for(i=1;i<Maxn;i++)
   {
    S[i]+=S[i-1];
    if(A=Mu[i])
      {

         for(B=tot=0,j=i;j^1;j/=Factor[j])Cache[++tot]=Factor[j];           
         Search(1,1);
         S[i]+=B*Mu[i];
      }
   }
  read(T);
  while(T--)
  {  
    read(n);
    printf("%lld\n",S[n]); 
  }

  return 0; 
} 

你可能感兴趣的:(BZOJ3739: DZY loves math VIII)