UVA 10325(容斥原理+简单题)

题意:给定n,m和m个数字,找出1到n中的不被m个数字中任何一个整除的个数;

运用容斥原理,Ai(1<=i<=m)表示1到n中被M[i]整除的个数则可以得到答案;

注意同时被k个数字整除的个数为n/(k个数字的lcm);

 1 #include<iostream>
 2 #include<string.h>
 3 #include<string>
 4 #include<sstream>
 5 #include<vector>
 6 #include<deque>
 7 #include<map>
 8 #include<algorithm>
 9 #include<iomanip>
10 #include<math.h>
11 #include<set>
12 using namespace std;
13 
14 typedef long long ll;
15 typedef unsigned long long ull;
16 
17 ll se[15];
18 ll gcd(ll a, ll b)
19 {
20     return a % b ? gcd(b, a%b) : b;
21 }
22 ll lcm(ll a, ll b)
23 {
24     return a * b / gcd(a, b);
25 }
26 int main()
27 {
28     ll n, m;
29     while (cin >> n >> m)
30     {
31         for (int i = 0; i < m; i++)
32             cin >> se[i];
33         ll sum = 0;
34         for (int i = 1; i < (1 << m); i++)
35         {
36             ll ans = 1;
37             int bit = 0;
38             for (int j = 0; j < m; j++)
39             {
40                 if ((1 << j)&i)
41                 {
42                     ans = lcm(ans, se[j]);
43                     bit++;
44                 }
45             }
46             ll cnt = n / ans;
47             if (bit % 2 == 0)
48             {
49                 sum = sum - cnt;
50             }
51             else
52                 sum = sum + cnt;
53         }
54         cout << n - sum << endl;
55     }
56     return 0;
57 }

猜你喜欢

转载自www.cnblogs.com/QingFengDaHui/p/10392676.html
今日推荐