How many integers can you find

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

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
 
Source
思路:最简单的容斥,注意下可能输入0;奇加偶减
   比如12 2 
              2   3
     ans=11/2+11/3-11/6;
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<string>
  5. #include<queue>
  6. #include<algorithm>
  7. #include<stack>
  8. #include<cstring>
  9. #include<vector>
  10. #include<list>
  11. #include<set>
  12. #include<map>
  13. using namespace std;
  14. #define ll __int64
  15. #define mod 1000000007
  16. #define inf 999999999
  17. //#pragma comment(linker, "/STACK:102400000,102400000")
  18. int scan()
  19. {
  20. int res = , ch ;
  21. while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
  22. {
  23. if( ch == EOF ) return << ;
  24. }
  25. res = ch - '' ;
  26. while( ( ch = getchar() ) >= '' && ch <= '' )
  27. res = res * + ( ch - '' ) ;
  28. return res ;
  29. }
  30. ll a[];
  31. ll ji;
  32. ll ans,x,y;
  33. ll gcd(ll x,ll y)
  34. {
  35. return y==?x:gcd(y,x%y);
  36. }
  37. void dfs(ll lcm,ll pos,ll step)
  38. {
  39. if(lcm>x)
  40. return;
  41. if(pos==ji)
  42. {
  43. if(step==)
  44. return;
  45. if(step&)
  46. ans+=x/lcm;
  47. else
  48. ans-=x/lcm;
  49. return;
  50. }
  51. dfs(lcm,pos+,step);
  52. dfs(lcm/gcd(a[pos],lcm)*a[pos],pos+,step+);
  53. }
  54. int main()
  55. {
  56. ll z,i,t;
  57. while(~scanf("%I64d%I64d",&x,&y))
  58. {
  59. x--;
  60. ji=;
  61. for(i=;i<y;i++)
  62. {
  63. scanf("%I64d",&z);
  64. if(z==)continue;
  65. a[ji++]=z;
  66. }
  67. ans=;
  68. dfs(,,);
  69. printf("%I64d\n",ans);
  70. }
  71. return ;
  72. }

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 容斥入门

    How many integers can you find Problem Description   Now you get a number N, and a M-integers set, y ...

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

    题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n ...

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

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

  5. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. HDU1796 How many integers can you find【容斥定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包 ...

  8. hdu 4135 [a,b]中n互质数个数+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...

  9. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. [py]letcode第一题求和

    letcode第一题, tm的不好弄. 想了很久想到了一个粗蠢的解决办法. Given an array of integers, return indices of the two numbers ...

  2. [LeetCode] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  3. [LeetCode] 675. Cut Off Trees for Golf Event_Hard tag: BFS

    You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...

  4. numpy的ravel()和flatten()函数

    相同点: 两者所要实现的功能是一致的(将多维数组降位一维).这点从两个单词的意也可以看出来,ravel(散开,解开),flatten(变平). In [14]: x=np.array([[1,2],[ ...

  5. C:\WINDOWS\system32\drivers\etc\hosts 文件的作用

    先来看一看C:\WINDOWS\system32\drivers\etc\hosts 系统原来的hosts文件(未经过改动) 打开原来的hosts文件,查看原来的内容 host是一个没有扩展名的系统文 ...

  6. servlet07

    1.session验证 可以防止非登录的用户,通过在地址栏中输入地址,访问受保护的页面 step1.在用户登录成功之后,将用户的信息保存到session中 step2.在访问受保护的页面时,校验ses ...

  7. 9/24matplotlib简介

    Matplotlib是一个在python下实现的类matlib的纯python的三方库,旨在用python实现matlib的功能,是python下最出色的绘图库,功能很完善,其风格根matlib很相似 ...

  8. Problem B. Full Binary Tree

    题目 链接:http://code.google.com/codejam/contest/2984486/dashboard#s=p1 googlde code jam 2014 Round1A 解题 ...

  9. SQL 中单引号 和一些特殊字符的处理

    为了防止程序SQL语句错误以及SQL注入,单引号必须经过处理.有2种办法: 1.使用参数,比如SELECT * FROM yourTable WHERE name = @name; 在Java中就是用 ...

  10. PowerDesigner 表格导出为excel

    PD菜单栏中,依次点击 Tools ->Excute Commands->Edit/Run Script.. 填入 '*********************************** ...