How many integers can you find

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

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
思路:容斥原理;需要注意的是给你的数有可能包含0,只要把0换成比n-1大的数或者去掉就行;
还有求的是<n的,那么这时麻烦的地方就是要判断整除,所以转变下就是求(<=n-1)就行这时不需要判断是否整除。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<vector>
7 #include<queue>
8 #include<stack>
9 using namespace std;
10 long long gcd(long long n,long long m);
11 int ans[20];
12 int main(void)
13 {
14 int i,j,k;
15 int n,m;
16 while(scanf("%d %d",&n,&m)!=EOF)
17 {
18 int sum=0;
19 n=n-1;
20 for(i=0; i<m; i++)
21 {
22 scanf("%d",&ans[i]);
23 }
24 for(i=0; i<m; i++)
25 {
26 if(ans[i]==0)
27 ans[i]=n+1;
28 }
29 for(i=1; i<=(1<<m)-1; i++)
30 {
31 int cnt=0;
32 long long an=1;
33 int flag=0;
34 for(j=0; j<m; j++)
35 {
36 if(i&(1<<j))
37 {
38 cnt++;
39 long long cc=gcd(an,(long long)ans[j]);
40 an=an/cc*ans[j];
41 if(an>n)
42 {
43 flag=1;
44 break;
45 }
46 }
47 }
48 if(flag)
49 continue;
50 else
51 {
52 if(cnt%2)
53 sum+=n/(int)an;
54 else sum-=n/(int)an;
55 }
56 }
57 printf("%d\n",sum);
58 }
59 return 0;
60 }
61 long long gcd(long long n,long long m)
62 {
63 if(m==0)
64 return n;
65 else if(n%m==0)
66 return m;
67 else return gcd(m,n%m);
68 }

How many integers can you find(hdu1796)的更多相关文章

  1. hdu1796 How many integers can you find 容斥原理

    Now you get a number N, and a M-integers set, you should find out how many integers which are small ...

  2. Hdu1796 How many integers can you find 2017-06-27 15:54 25人阅读 评论(0) 收藏

    How many integers can you find Time Limit : 12000/5000ms (Java/Other)   Memory Limit : 65536/32768K ...

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

    题目给一个数字集合,问有多少个小于n的正整数能被集合里至少一个元素整除. 当然是容斥原理来计数了,计算1个元素组合的有几个减去2个元素组合的LCM有几个加上3个元素组合的LCM有几个.注意是LCM. ...

  4. hdu1796 How many integers can you find

    //设置m,Q小于n可以设置如何几号m随机多项整除 //利用已知的容斥原理 //ans = 数是由数的数目整除 - 数为整除的两个数的数的最小公倍数 + 由三个数字... #include<cs ...

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

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

  6. [LeetCode] Sum of Two Integers 两数之和

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  7. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

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

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

  9. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

随机推荐

  1. java类加载、对象创建过程

    类加载过程: 1, JVM会先去方法区中找有没有相应类的.class存在.如果有,就直接使用:如果没有,则把相关类的.class加载到方法区 2, 在.class加载到方法区时,会分为两部分加载:先加 ...

  2. Hadoop、Hive【LZO压缩配置和使用】

    目录 一.编译 二.相关配置 三.为LZO文件创建索引 四.Hive为LZO文件建立索引 1.hive创建的lzo压缩的分区表 2.给.lzo压缩文件建立索引index 3.读取Lzo文件的注意事项( ...

  3. C++之无子数

    题目如下: 1 #include <iostream> 2 3 using namespace std; 4 5 6 bool isThisNumhaveChild(int num); 7 ...

  4. 时光网内地影视票房Top100爬取

    为了和艺恩网的数据作比较,让结果更精确,在昨天又写了一个时光网信息的爬取,这次的难度比艺恩网的大不少,话不多说,先放代码 # -*- coding:utf-8 -*-from __future__ i ...

  5. Mysql资料 主键

    目录 一.简介 二.操作 三.技巧 一.简介 主键意味着表中每一行都应该有可以唯一标识自己的一列(或一组列). 一个顾客可以使用顾客编号列,而订单可以使用订单ID,雇员可以使用雇员ID 或 雇员社会保 ...

  6. inode节点

    目录 一.简介 二.信息 inode的内容 inode的大小 3.inode号码 三.目录文件 四.硬连接 五.软链接 六.inode的特殊作用 一.简介 理解inode,要从文件储存说起. 文件储存 ...

  7. shell脚本 双向登陆免密

    一.简介 源码地址 日期:2018/4/23 介绍:用于hadoop的双向免密脚本,让填写机器互相之间免密登陆 效果图: 暂无 二.使用 适用:centos6+ 语言:中文 注意:执行前需要填写脚本里 ...

  8. Dom 解析XML

    xml文件 <?xml version="1.0" encoding="UTF-8"?><data>    <book id=&q ...

  9. [BUUCTF]PWN——jarvisoj_fm

    jarvisoj_fm 附件 步骤: 例行检查,32位,开启了canary和nx保护 运行一下程序,看看大概的情况 32位ida载入,shift+f12检索程序里的字符串,看见了 " /bi ...

  10. java JDK8 时间处理

    目录 时间格式化 LocalDate:年月日 LocalTime:时分秒毫秒 LocalDateTime:年月日时分秒 Instant:纳秒时间戳 Duration:两时间间隔 Duration:处理 ...