题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002

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

启发博客:http://www.cnblogs.com/jackge/archive/2013/04/03/2997169.html

题目大意:给定n和一个大小为m的集合,集合元素为非负整数。为1...n内能被集合里任意一个数整除的数字个数。n<=2^31,m<=10

 

解题思路:容斥原理地简单应用。先找出1...n内能被集合中任意一个元素整除的个数,再减去能被集合中任意两个整除的个数,即能被它们两只的最小公倍数整除的个数,因为这部分被计算了两次,然后又加上三个时候的个数,然后又减去四个时候的倍数...所以深搜,最后判断下集合元素的个数为奇还是偶,奇加偶减。

 #include<cstdio>
#include<iostream>
using namespace std; long long a[];
long long ans;
int cnt;
int n,m; long long gcd(long long b,long long c)//计算最大公约数
{
return c==?b:gcd(c,b%c);
} long long lcm(long long b,long long c)//计算最小公倍数
{
return b * c/ gcd(b, c);
} void dfs(int cur,int num,long long Lcm)
//深搜,搜出每一种数学组合的可能,因为m<=10所以不会爆
{
Lcm=lcm(Lcm,a[cur]);
if(num%==)
ans-=(n-)/Lcm;
else
ans+=(n-)/Lcm;
for(int j=cur+;j<cnt;j++)//这个j只能放在里面定义!!
dfs(j,num+,Lcm);
}
//cur指当前数字在数组中的位置,num指目前计算公倍数的数字是偶是奇,Lcm指目前计算出的最小公倍数 int main()
{
while(~scanf("%d%d",&n,&m))
{
cnt=;
int x;
while(m--)
{
scanf("%d",&x);
if(x!=)//除去0的那种情况
a[cnt++]=x;
}
ans=;
for(int i=;i<cnt;i++)
dfs(i,,);
//容斥,奇加偶减
printf("%lld\n",ans);
}
return ;
}

HDU 1796 How many integers can you find(容斥原理)的更多相关文章

  1. 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 ...

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

    题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...

  3. HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)

    HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...

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

    题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 ...

  5. 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, y ...

  6. 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 ...

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

  8. 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 ...

  9. hdu 1796 How many integers can you find

    容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #i ...

随机推荐

  1. blog项目知识点梳理

    1.获取图片验证码: def get_validCode_img(request): # 方式1: # import os # path= os.path.join(settings.BASE_DIR ...

  2. python中sorted和.sorted 、reversed和reverse的注意点

    L=[1,2,3,4]l1=[123,123,23]if l1.sort() == L.reverse():   #这个判断式是恒等的,因为两个函数的返回值都是None(其实是无返回值)    pri ...

  3. MySql(七)多表查询

    十一.多表查询 新建两张表:部门表(department).员工表(employee) create table department( id int, name varchar(20) ); cre ...

  4. 浙江省赛 ZOJ4029

    Now Loading!!! Time Limit: Second Memory Limit: KB DreamGrid has integers . DreamGrid also has queri ...

  5. php-fpm占用cpu和内存过高100% 解决办法

    参考网站: https://www.fujieace.com/php/php-fpm.html https://www.fujieace.com/php/pm-max_children-2.html ...

  6. Sql server中 如何用sql语句创建视图

    1.视图的作用 视图的作用: 第一点:使用视图,可以定制用户数据,聚焦特定的数据. 解释: 在实际过程中,公司有不同角色的工作人员,我们以销售公司为例的话, 采购人员,可以需要一些与其有关的数据,而与 ...

  7. H5离线缓存技术Application Cache

    H5离线缓存技术Application Cache 1.离线缓存技术:是浏览器本身的一种机制 HTML5引入Application Cache(应用程序缓存)技术,离线存储可以将站点的一些文件存储在本 ...

  8. learning shell display alert function

    [Purpose]        Shell print function base on err info wrn ext output level   [Eevironment]        U ...

  9. Java中的equals和==的差别 以及Java中等价性和同一性的讨论

    ==对基本数据类型比较的是值,对引用类型比较的是地址 equals()比较的是对象的数据的引用 等价性原理: 自反性    x.equals(x)为true 对称性    x.equals(y) 为t ...

  10. react+dva+antd/antd-mobile

    github仓库pc: https://github.com/llcMite/react-dva-antd.git github仓库mobile:https://github.com/llcMite/ ...