How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5664    Accepted Submission(s): 1630

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
题意:在m个数的集合中,问在小于n的范围内有多少个数能整除m个数的集合中的数。
收获: 容斥原理两种表示方法: 1.二进制, 2.递归
1.二进制
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <string>
  8. #include <cstring>
  9. #include <stack>
  10. #include <queue>
  11. #include <list>
  12. #include <vector>
  13. #include <map>
  14. #include <set>
  15. using namespace std;
  16.  
  17. const int INF=0x3f3f3f3f;
  18. const double eps=1e-;
  19. const double PI=acos(-1.0);
  20. #define maxn 500
  21. __int64 k[maxn];
  22. __int64 gcd(__int64 b,__int64 a)
  23. {
  24. return a==?b:gcd(a,b%a);
  25. }
  26. int main()
  27. {
  28. int n, m;
  29. while(~scanf("%d%d", &n, &m))
  30. {
  31. int t;
  32. n--;
  33. int cnt = ;
  34. for(int i = ; i < m; i++)
  35. {
  36. scanf("%d", &t);
  37. if(t> && t < n)
  38. k[cnt++] = t;
  39. }
  40. __int64 ans = ;
  41. for(int i = ; i < <<cnt; i++)
  42. {
  43. int num = ;
  44. __int64 lcm = ;
  45. for(int j = ; j < cnt; j++)
  46. {
  47. if(i & ( << j))
  48. {
  49. num++;
  50. lcm = k[j]/gcd(k[j], lcm) * lcm;
  51. }
  52. }
  53. if(num & )
  54. ans += n/lcm;
  55. else
  56. ans -= n/lcm;
  57. }
  58. printf("%I64d\n", ans);
  59. }
  60. return ;
  61. }

2. 递归

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6. __int64 a[];
  7. int n,m;
  8. //cur表示
  9. __int64 sum=;
  10. __int64 gcd(__int64 b,__int64 a)
  11. {
  12. return a==?b:gcd(a,b%a);
  13. //while()
  14. }//最小公倍数
  15. void dfs(int cur,__int64 lcm,int id)//容斥原理公式
  16. {
  17. //lcm=lcm/gcd(lcm,a[cur])*a[cur];
  18. lcm=a[cur]/gcd(a[cur],lcm)*lcm;
  19. if(id&)//运用了快速幂的方法判断奇偶
  20. sum+=(n-)/lcm;
  21. else
  22. sum-=(n-)/lcm;
  23. // cout<<"id = "<<id<<" : "<<sum<<endl;
  24. for(int i=cur+;i<=m;i++)
  25. dfs(i,lcm,id+);
  26. }
  27. int main()
  28. {
  29. int t;
  30. while(~scanf("%d%d",&n,&t))
  31. {
  32. int i,x;
  33. m=;
  34. for(i=;i<=t;i++)
  35. {
  36. scanf("%d",&x);
  37. if(x)
  38. {
  39. a[++m]=x;
  40. }
  41. }
  42. sum=;
  43. for(i=;i<=m;i++)
  44. dfs(i,a[i],);
  45. printf("%I64d\n",sum);//容斥原理公式
  46. // cout<<sum<<endl;
  47. }
  48. return ;
  49. }

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

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

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

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

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

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

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

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

    题目连接   http://acm.hdu.edu.cn/showproblem.php?pid=1796 处男容斥原理  纪念一下  TMD看了好久才明白DFS... 先贴代码后解释 #includ ...

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

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

  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

    题意: 给一个N.然后给M个数,问1~N-1里面有多少个数能被这M个数中一个或多个数整除. 思路: 首先要N-- 然后对于每一个数M 事实上1~N-1内能被其整除的 就是有(N-1)/M[i]个 可是 ...

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

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

随机推荐

  1. IIS缺少文件的解决方法

    原文 http://cqyccmh.blog.163.com/blog/static/6068134720102211543944/ 今天解决了一个郁闷了很久的问题,之前实在没辙就只能重装系统,因为装 ...

  2. 电机转矩T=9550*P/N推导。

    很奇怪,这个公式怎么来的,原来好多是基础物理的,也许我们初中高中物理书上多有,基础真的是很基础的基础. P=F*V (1)  ,即功率=力*速度 T=F*R (2) ,即力矩=力*作用长度 ,在电机里 ...

  3. Java开发者工具

    From:http://www.csdn.net/article/2015-03-26/2824317 1. Notepad++ Notepad++是用于编辑xml.脚本以及记笔记的最佳工具.这个工具 ...

  4. C++ 线程的创建,挂起,唤醒,终止

    例子: 线程代码: DWORD __stdcall ThreadProc(LPVOID lpParameter) { CMultiThreadDlg * pdlg = (CMultiThreadDlg ...

  5. 【Android】使用FrameLayout布局实现霓虹灯效果

    FrameLayout是五大布局中最简单的一个布局. 在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置. 它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的 ...

  6. JAXB 注解

    JAXB(Java API for XML Binding),它提供了一个便捷的方式高速Java对象XML转变.于JAX-WS(Java的WebService规范之中的一个)中,JDK1.6 自带的版 ...

  7. C++实现20个设计模式

    http://c.chinaitlab.com/special/sjms/Index.html 一个月下来,把常见的20个设计模式好好复习并且逐个用C++实现了一遍,收获还是很大的,很多东西看上去明白 ...

  8. MySql5.1在Win7下的安装与重装问题的解决

    痛苦啊痛苦,我也不知道这两天怎么了.上班没有精神,还打瞌睡,下班后又感觉很累.精力集中不起来. 这篇花了我好久的时间,我效率这么差,~\(≧▽≦)/~. 软件包下载 首先单击mysql-5.1.53- ...

  9. C# 调用存储过程传入表变量作为参数

    首先在SQLServer定义一个自定义表类型: USE [ABC] GO CREATE TYPE [ABC].[MyCustomType] AS TABLE( ) NOT NULL, ) NULL, ...

  10. RabbitMQ消息队列安装和配置以及推送消息

    好久没有写了,最近项目用到RabbitMQ,找了一些资料试验,最后终于成功了,把安装配置的步骤分享给大家. 一.Erlang安装具体过程: 1.双击otp_win32_R16801.exe(不同版本可 ...