您的位置:首页 > 其它

hdu1796 How many integers can you find 容斥原理

2017-03-20 23:29 447 查看
Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.

容斥原理裸题

1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<math.h>
5 using namespace std;
6 typedef long long ll;
7
8 inline int gcd(int a,int b){
9     return b?gcd(b,a%b):a;
10 }
11
12 int num[15];
13
14 int main(){
15     int n,m;
16     while(scanf("%d%d",&n,&m)!=EOF){
17         n--;
18         for(int i=1;i<=m;++i){
19             scanf("%d",&num[i]);
20             if(!num[i]){
21                 i--;
22                 m--;
23             }
24         }
25         ll ans=0;
26         for(int i=1;i<(1<<m);++i){
27             int bit=0;
28             int tmp=1;
29             for(int j=1;j<=m;++j){
30                 if(i&(1<<(j-1))){
31                     bit++;
32                     tmp=tmp/gcd(tmp,num[j])*num[j];
33                 }
34             }
35             if(bit%2)ans+=n/tmp;
36             else ans-=n/tmp;
37         }
38         printf("%lld\n",ans);
39     }
40     return 0;
41 }


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: