ahu-557容斥原理

来源:互联网 发布:js onclick传多个参数 编辑:程序博客网 时间:2024/06/05 15:35

用容斥求1-n中能被2-i中素数整除的个数ans其中 i*i<=n;然后结果为n-ans-1;

#include<cstdio>int pri[10005],pri2[10005],i1,n,ans;void toGetPrim(){    for(int i=2;i<=10001;i++)     if(!pri[i]){        for(int j=2;j*i<=10001;j++)         pri[i*j] = 1;       }     i1 = 0;     for(int i=2;i<=10001;i++)     if(!pri[i]){       pri2[i1] = i;       i1++;      }}void dfs(int re,int all,int pre){      if(re!=1)      {      if(all==1)ans-=1;      if(all%2)ans+=n/re;      else ans-=n/re;      }      for(int i=pre;i<i1;i++){        long long g = (long long)re*pri2[i];        if(g>n)return;        dfs(g,all+1,i+1);      }}int main(){    toGetPrim();    while(scanf("%d",&n)&&n)    {        ans = 0;        dfs(1,0,0);        printf("%d\n",n-1-ans);    }    return 0;}
0 0
原创粉丝点击