How many integers can you find

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

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
题意:给n个数字,最大不会超过20的非负数,0忽略它可以。给你一个数字M,
问1-M-1中,有多少个数字能被这n数字中任何一个整除(只要满足其中一个能整除就行)。统计个数输出。
 
思路:容斥,简单容斥。一开始做zoj的一道题,果断数据太水,方法是不对的也能ac。
原来的思路是这样的,对n个数字,筛选掉ai倍数的数字,然后就容斥,但是明显这样的数据有问题
4 6,  这样容斥后得到的结果是4 6 -24,不对的,应该是4 6 -12,所以应该是 4 6    -(4*6)/gcd(4,6)

略坑略坑。

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<cstdlib>
  5. using namespace std;
  6.  
  7. bool Hash[];
  8. int f[],len,qlen;
  9. __int64 Q[];
  10.  
  11. int gcd(int a,int b)
  12. {
  13. if(a<)a=-a;
  14. if(b<)b=-b;
  15. if(b==)return a;
  16. int r;
  17. while(b)
  18. {
  19. r=a%b;
  20. a=b;
  21. b=r;
  22. }
  23. return a;
  24. }
  25. void solve(__int64 m)
  26. {
  27. qlen = ;
  28. Q[]=-;
  29. for(int i=;i<=len;i++)
  30. {
  31. int k=qlen;
  32. for(int j=;j<=k;j++)
  33. Q[++qlen]=-*(Q[j]*f[i]/gcd(Q[j],f[i]));
  34. }
  35. __int64 sum = ;
  36. for(int i=;i<=qlen;i++)
  37. sum = sum+m/Q[i];
  38. printf("%I64d\n",sum);
  39. }
  40. int main()
  41. {
  42. int m,x;
  43. __int64 n;
  44. while(scanf("%I64d%d",&n,&m)>)
  45. {
  46. n=n-;
  47. memset(Hash,false,sizeof(Hash));
  48. for(int i=;i<=m;i++)
  49. {
  50. scanf("%d",&x);
  51. Hash[x]=true;
  52. }
  53. for(int i=;i<=;i++)
  54. {
  55. if(Hash[i]==true)
  56. for(int j=i+i;j<=;j=j+i)
  57. if(Hash[j]==true) Hash[j]=false;
  58. }
  59. len = ;
  60. for(int i=;i<=;i++)if(Hash[i]==true) f[++len]=i;
  61. solve(n);
  62. }
  63. return ;
  64. }

HDU 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 Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

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

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

  4. How many integers can you find(容斥+dfs容斥)

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

  5. hdu 6169 Senior PanⅡ Miller_Rabin素数测试+容斥

    Senior PanⅡ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pr ...

  6. HDU - 5297:Y sequence (迭代&容斥)

    Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hat ...

  7. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  8. HDU - 4336:Card Collector(min-max容斥求期望)

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  9. HDU - 5977 Garden of Eden (树形dp+容斥)

    题意:一棵树上有n(n<=50000)个结点,结点有k(k<=10)种颜色,问树上总共有多少条包含所有颜色的路径. 我最初的想法是树形状压dp,设dp[u][S]为以结点u为根的包含颜色集 ...

随机推荐

  1. ACM之Java速成(2)

    acm中Java的应用 Chapter I. Java的优缺点各种书上都有,这里只说说用Java做ACM-ICPC的特点: (1) 最明显的好处是,学会Java,可以参加Java Challenge ...

  2. 常见的appbug(转)

    移动App Bug的影响是用户体验差.App的商店评级下降.用户换用竞争对手的App,声誉和信誉损失.最后销售量减少,如果它是一个付费App的话. 移动App测试与传统台式机测试相比有一定的复杂性.这 ...

  3. HDU 4899 Hero meet devil(状压DP)(2014 Multi-University Training Contest 4)

    Problem Description There is an old country and the king fell in love with a devil. The devil always ...

  4. C语言 类型

    int 2个字节或4个字节 根据平台而定, -32,768 到 32,767 或 -2,147,483,648 到 2,147,483,647 unsigned int 2或4个字节    0到655 ...

  5. JS获取项目根目录

    function getRootPath(){ //获取当前网址,如: http://localhost:8088/test/test.jsp var curPath=window.document. ...

  6. 基于fullpage的幻灯片播放

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...

  7. Script to compile invalid objects in DB

    REM: Script to compile invalid objects in DB after refreshing REM: REM:***************************** ...

  8. Java中的数组操作进阶

    package com.mi.array; import java.util.Arrays; /** * System.arraycopy(i, 0, j, 0, i.length);这种复制会覆盖目 ...

  9. 夺命雷公狗---node.js---3commonJs 与 nodeJs的简介

    JavaScript是一个强大面向对象语言,它有很多快速高效的解释器.官方JavaScript标准定义的API是为了构建基于浏览器的应用程序.然而,并没有定于一个用于更广泛的应用程序的标准库. Com ...

  10. RobotFrameWork接口报文测试-----(一)简单demo的实现

    最近几个月的工作任务都是通过使用RF工具来搭建服务器端接口的自动化测试,使用python作为2次开发的语言,也是第一次去做这种项目,经验善浅,还是很有可能会走很多的弯路,为此,我希望自己能把每个阶段的 ...