题目传送: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. java利用jxl实现Excel导入功能

    本次项目实践基于Spring+SpringMvc+MyBatis框架,简单实现了Excel模板导出.和Excel批量导入的功能.实现过程如下:. 1.maven导入所需jar包 <depende ...

  2. 安卓——Activity生命周期

      在xml 设计页面添加标签 xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  3. 1003. Check If Word Is Valid After Substitutions Medium检查替换后的词是否有效

    网址:https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/ 参考:https://leetcode.com ...

  4. CSS3透明背景+渐变样式

    CSS3透明背景+渐变样式 转载自博文:<CSS3透明背景+渐变样式> http://blog.csdn.net/netbug_nb/article/details/44343809 效果 ...

  5. [CodeForces - 197A] A - Plate Game

    A - Plate Game You've got a rectangular table with length a and width b and the infinite number of p ...

  6. Kali安装教程(VMWare)

    1.下载镜像及相关 1.1下载镜像文件 下载链接:https://www.kali.org/downloads/ 选择自己需要的版本下载,根据经验先下载种子文件(torrent)再用迅雷下载网速是最有 ...

  7. EXCEL词典(xllex.dll)文件丢失或损坏解决方法

    EXCEL词典(xllex.dll)文件丢失或损坏解决方法     1● 问题   2● 解决 fail 3● 方法2   regsvr32 xllex.dll     4● 方法3 启动server ...

  8. Easyui的datagrid的editor(行编辑器)如何扩展datetimebox类型

    在easyui的datagrid扩展方法中添加这样的时间日期(datetimebox)代码块 放在   $.extend($.fn.datagrid.defaults.editors,{datetim ...

  9. 回声UDP服务器端/客户端

    UDP是具有数据边界的协议,传输中调用I/O函数的次数非常重要.输入函数的调用次数要和输出函数的调用次数完全一致,这样才能保证接受全部已发送的数据. TCP套接字中需注册待传输数据的目标IP和端口,而 ...

  10. beamer template

    \setbeamercolor{postit}{fg=black,bg=white} \begin{beamercolorbox}[rounded=true,shadow=true, sep=0em, ...