1. 程式人生 > >容斥+dfs求1~n質數個數

容斥+dfs求1~n質數個數

n<=100000000

  #include<stdio.h>
 #include<string.h>
 #include<math.h>

int flag[10006];
 int num[10000],total,now,n;

void solve(int index,int Mul,int K){
       int i,t;
    if(K==0){
     now+=n/Mul;
     return;
    }
    for(i=index;i<total-K+1;i++){
      Mul*=num[i];
      t=now;
      if
(Mul<=n){ solve(i+1,Mul,K-1); } if(t==now)//優化剪枝(1return; Mul/=num[i]; } } int Judge(int x){ int t,i; t=sqrt(x*1.0); for(i=2;i<=t;i++) if(n%i==0) return 0; return 1; } int main(){ int i,j,t,Count; memset(flag,0,sizeof(flag)); for
(i=2;i<=10000;i++){ if(flag[i]) continue; for(j=2;j*i<=10000;j++) flag[i*j]=1; } while(scanf("%d",&n)==1){ Count=0; if(n<=10000){ for(i=2;i<n;i++) if(!flag[i]) Count++; } else{ t=sqrt(1.0*n); total=0; for(i=2;i<=t;i++) if(!flag[i]) num[total++]=i; for
(i=1;i<=total;i++){ now=0; solve(0,1,i); if(now==0)//優化(2) break; if(i&1) Count+=now; else Count-=now; } Count-=total; Count=n-Count-1; if(Judge(n)) Count--; } printf("%d\n",Count); } return 0; }