题目传送: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. 【LeetCode】大数相乘

    1. 模拟手工计算 原理: 将 string 反转存储在 int 数组中,如 A = 17 = (7, 1),B = 25 = (5, 2),亦即幂表示法,幂次是从低位到高位. 作逐位相乘,即 ai ...

  2. Django框架(一)

    一.web应用 web应用 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件.应用程序有两种模式C/S.B/S.C ...

  3. 【九校联考-24凉心模拟】锻造(forging)

    题目背景 勇者虽然武力值很高,但在经历了多次战斗后,发现怪物越来越难打, 于是开始思考是不是自己平时锻炼没到位,于是苦练一个月后发现……自 己连一个史莱姆都打不过了. 勇者的精灵路由器告诉勇者其实是他 ...

  4. Legal or Not (判断是否存在环)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  5. centos7安装nginx 报./configure: error: C compiler cc is not found

    CentOS 7 下 安装 nginx 执行配置命令 ./configure 时提示以下错误: 解决: 执行以下命令: yum -y install gcc gcc-c++ autoconf auto ...

  6. 记录一下ES6扩展运算符(三点运算符)...的用法

    ...运算符用于操作数组,有两种层面 1. 第一个叫做 展开运算符(spread operator),作用是和字面意思一样,就是把东西展开.可以用在array和object上都行. 比如: let a ...

  7. Apache支持TRACE请求漏洞处理方案

    trace和get一样是http的一种请求方法,该方法的作用是回显收到的客户端请求,一般用于测试服务器运行状态是否正常. 该方法结合浏览器漏洞可能造成跨站脚本攻击.修复方法如下: 编缉/etc/htt ...

  8. Qt绘制文本二 弯曲排列和旋转效果 弧形路径 正弦函数路径

    void WgtText::paintEvent(QPaintEvent *event) { QPainter painter(this); QString m_string("abcdef ...

  9. oracle的case when的用法和decode函数的用法

    oracle中,我们要使用case when时,要怎样使用 如下测试数据,我要把ClassId 的1变成一班,2变成二班,3变成三班,那sql要怎样写呢? 1.case when的用法 -- orac ...

  10. mysql索引类型和方式

    索引 数据库的索引就像一本书的目录,能够加快数据库的查询速度. MYSQL索引有四种PRIMARY.INDEX.UNIQUE.FULLTEXT, 其中PRIMARY.INDEX.UNIQUE是一类,F ...