HDU 1796 容斥原理

Posted 半根毛线code

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1796 容斥原理相关的知识,希望对你有一定的参考价值。

How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7439    Accepted Submission(s): 2200


Problem Description
  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.
 

 

Input
  There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.
 

 

Output
  For each case, output the number.
 

 

Sample Input
12 2
2 3
 

 

Sample Output
7
 

 

Author
wangye
 

 

Source
 
题意:给你m个数 可能含有0   问有多少小于n的正数能整除这个m个数中的某一个
题解:特判除零  容斥原理   这m个数 组合时不能直接相乘 应当取最小公倍数
 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 #include<bits/stdc++.h>
 8 #include<map>
 9 #include<set>
10 #include<cmath>
11 #include<queue>
12 #include<bitset>
13 #include<math.h>
14 #include<vector>
15 #include<string>
16 #include<stdio.h>
17 #include<cstring>
18 #include<iostream>
19 #include<algorithm>
20 #pragma comment(linker, "/STACK:102400000,102400000")
21 using namespace std;
22 #define  A first
23 #define B second
24 const int mod=1000000007;
25 const int MOD1=1000000007;
26 const int MOD2=1000000009;
27 const double EPS=0.00000001;
28 typedef __int64 ll;
29 const ll MOD=1000000007;
30 const int INF=1000000010;
31 const ll MAX=1ll<<55;
32 const double eps=1e-8;
33 const double inf=~0u>>1;
34 const double pi=acos(-1.0);
35 typedef double db;
36 typedef unsigned int uint;
37 typedef unsigned long long ull;
38 ll gcd(ll aa,ll bb)
39 {
40     if(bb==0)
41         return aa;
42     else
43         return gcd(bb,aa%bb);
44 }
45 ll lcm(ll aa,ll bb)
46 {
47     return aa*bb/gcd(aa,bb);
48 }
49 ll n,m;
50 ll que[15];
51 ll a[10000];
52 ll ggg;
53 ll coun;
54 ll slove(ll gg)
55 {
56     ll t=0,sum=0;
57     a[t++]=-1;
58     for(ll i=0;i<coun;i++)
59     {
60 
61         ll k=t;
62         for(ll j=0;j<k;j++)
63          {
64              a[t++]=que[i]*a[j]*(-1);
65              if(a[t-1]>0)
66                  a[t-1]=lcm(que[i],-a[j]);
67             else
68                  a[t-1]=0-lcm(que[i],a[j]);
69          }
70     }
71     for(ll i=1;i<t;i++)
72             sum=sum+(gg/a[i]);
73     return sum;
74 }
75 int main()
76 {
77     while(scanf("%I64d %I64d",&n,&m)!=EOF)
78     {
79         memset(que,0,sizeof(que));
80         memset(a,0,sizeof(a));
81         coun=0;
82         for(ll i=0;i<m;i++)
83            {
84                scanf("%I64d",&ggg);
85                if(ggg!=0)
86                 que[coun++]=ggg;
87            }
88         printf("%I64d\\n",slove(n-1));
89     }
90      return 0;
91 }

 

 

以上是关于HDU 1796 容斥原理的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1796How many integers can you find(容斥原理)

[容斥原理] hdu 1796 How many integers can you find

hdu1796 How many integers can you find 容斥原理

HDU 1796 How many integers can you find(容斥原理)

HDU 1796 How many integers can you find(容斥原理+二进制/DFS)

HDU 1796 How many integers can you find (容斥)