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

2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(4)

题意:给你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 }
时间: 2024-10-23 02:09:14

HDU 1796 容斥原理的相关文章

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): 6630    Accepted Submission(s): 1913 Problem Description Now you get a number N, and a M-integers set, you should

HDU 1796 How many integers can you find (状态压缩 + 容斥原理)

题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 } + sum{ 整除三个的数 }………………所以是奇加偶减,而整除 k 个数的数可以表示成 lcm(A1,A2,…,Ak) 的倍数的形式.所以算出最小公倍数, //HDU 1796 #include <cstdio> #include <iostream> #include <

HDU 1796 How many integers can you find(组合数学-容斥原理)

How many integers can you find 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

hdu 1695 容斥原理或莫比乌斯反演

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5310    Accepted Submission(s): 1907 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)

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

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5556    Accepted Submission(s): 1593 Problem Description Now you get a number N, and a M-integers set, you should

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

题意: 给一个N,然后给M个数,问1~N-1里面有多少个数能被这M个数中一个或多个数整除. 思路: 首先要N-- 然后对于每个数M 其实1~N-1内能被其整除的 就是有(N-1)/M[i]个 但是会出现重复 比如 样例 6就会被重复算 这时候我们就需要容斥原理了 加上一个数的减去两个数的.. 这里要注意了 两个数以上的时候 是求LCM而不是简单的相乘! 代码: #include "stdio.h" #include "string.h" #include "

容斥原理学习(Hdu 4135,Hdu 1796)

题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1412    Accepted Submission(s): 531 Problem Description Given a number N, you are asked to count the number of integers betwe

hdu 1796 How many integers can you find【容斥原理】

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4674    Accepted Submission(s): 1340 Problem Description Now you get a number N, and a M-integers set, you should

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

这个题的m的数中居然有0啊,RE了好几次.... 初学容斥原理,这才知道还有奇加偶减这个东西,以前一直以为容斥原理不过是把重复的删掉就好了,.. 然后知道奇加偶减这个东西后,就可以深搜了,将所有组合情况全列出来,然后求lcm就好了.数的个数就是(n-1)/lcm,虽然我代码里写的是gcd..不要在意这些细节... #include <iostream> #include <string.h> #include <math.h> #include <queue>