How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6434    Accepted Submission(s): 1849

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
  1. 12 2
  2. 2 3
 
Sample Output
  1.  
  2. 7
  3.  
  4. 这道题目是给定了因子,而且还不是互质的,
  5.  
  6. 所以在容斥原理上,就不能简单想乘,要求最小公倍数LCM
  7.  
  8. 另外有可能因子为0
  9. #include <iostream>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <algorithm>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. using namespace std;
  16. typedef long long int LL;
  17. LL gcd(LL a,LL b)
  18. {
  19. 	return b?gcd(b,a%b):a;
  20. }
  21. LL ans,a[15],n,m;
  22. void dfs(int id,int flag,int lcm)
  23. {
  24. 	lcm=a[id]/gcd(a[id],lcm)*lcm;
  25. 	if(flag)
  26. 		ans+=n/lcm;
  27. 	else
  28. 		ans-=n/lcm;
  29. 	for(int i=id+1;i<m;i++)
  30. 		dfs(i,!flag,lcm);
  31. }
  32. int main()
  33. {
  34. 	while(cin>>n>>m)
  35. 	{
  36.         n--;
  37. 		for(int i=0;i<m;i++)
  38. 		{
  39.             scanf("%d",&a[i]);
  40. 			if(!a[i]) i--,m--;
  41. 		}
  42. 		ans=0;
  43. 		for(int i=0;i<m;i++)
  44. 			dfs(i,1,a[i]);
  45. 		printf("%lld\n",ans);
  46. 	}
  47. 	return 0;
  48. }
  49.  

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

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

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

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

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

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

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

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

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

  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. mongodb - 查看正在执行的操作

    查看正在执行的操作 db.currentOp() 查看系统执行的操作 db.currentOp(True) kill正在执行的操作 db.killOp(<operation id>) 示例 ...

  2. JavaNIO - AbstractInterruptibleChannel

    1. 描述 可异步关闭和中断的Channel. (1)实现InterruptibleChannel接口的Channel支持异步关闭:如果一个线程IO阻塞在一个可中断的channel,另一个线程可以执行 ...

  3. [docker]bind9.11-with-mysql5.6 docker容器化实战

    参考: https://www.centos.bz/2012/09/bind-with-mysql-support/ http://blog.51niux.com/?id=125 http://470 ...

  4. c# 多线程里面创建byte数组发生内存溢出异常求解

    在多线程里面读取一个400多M的Xml文件,首先将其读入FileStream里面,然后,在执行 byte [] bts = new byte[fs.Length]; 这句代码时,出现内存溢出的异常,求 ...

  5. curl myip.ipip.net curl ip.cn curl cip.cc

    [命令行] curl查询公网出口IP 2016年07月22日 14:27:02 阅读数:19022 不管是在家里还是办公室,或者是公司的主机,很多时候都是在内网中,也就是说很多都是通过 NAT上网的, ...

  6. pom打包参数选择

    pom.xml配置 <profiles> <profile> <id>dev</id> <properties> <token> ...

  7. maven项目打ZIP包

    1.Maven插件配置: <!-- ZIP打包 --> <plugin> <artifactId>maven-assembly-plugin</artifac ...

  8. select 自定义样式插件 selectize.js

    [特别推荐]几款极好的 JavaScript 下拉列表插件   表单元素让人爱恨交加.作为网页最重要的组成部分,表单几乎无处不在,从简单的邮件订阅.登陆注册到复杂的需要多页填写的信息提交功能,表单都让 ...

  9. 在HTML5中如何提高网站前端性能

    1.    用web storage替换cookiesCookie最大的问题是每次都会跟在请求后面.在HTML5中,用sessionStorage和localStorage把用户数据直接在客户端,这样 ...

  10. Abstract Window Toolkit(AWT)是一个图形过程库

    Abstract Window Toolkit(AWT)是一个图形过程库,使用Java语言通过位图显示来操纵窗口.最后设计者又将AWT扩充为Alternative Window工具箱和Applet W ...