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. day09 orm查询优化相关

    day09 orm查询优化相关 今日内容概要 orm字段相关补充 orm查询优化相关 orm事务操作 图书管理系统练习 今日内容详细 orm事务操作 """ 事务:ACI ...

  2. C++基本函数的调用优化(构造、拷贝构造、赋值)

    合理的函数可提升时间和空间的利用率 //Test1.h #include<iostream> using namespace std; struct ST { private: int a ...

  3. mystar01 nodejs MVC 公共CSS,JS设置

    mystar01 nodejs MVC gulp 项目搭建 config/express.js中定义别名 //将下载的第三方库添加到静态资源路径当中,方便访问 app.use('/jquery', e ...

  4. 【Linux】【Web】【HTTP】HTTP,TCP,SSL通讯过程

    1. HTTP 一次完整的http请求处理过程: (1) 建立或处理连接:接收请求或拒绝请求(三次握手): (2) 接收请求:接收来自于网络上的主机请求报文中对某特定资源的一次请求的过程: (3) 处 ...

  5. Sentry 监控 - 私有 Docker Compose 部署与故障排除详解

    内容整理自官方开发文档 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Map ...

  6. Mysql从头部署多个版本

    目录 一.环境准备 二.下载安装包 三.Mysql-5.6单独部署 四.Mysql-5.7单独部署 五.添加到多版本控制 六.muliti使用 一.环境准备 系统:centos7.3一台 软件版本:m ...

  7. Offset函数(Excel函数集团)

    此处文章均为本妖原创,供下载.学习.探讨! 文章下载源是Office365国内版1Driver,如有链接问题请联系我. 请勿用于商业!谢谢 下载地址:https://officecommunity-m ...

  8. 小迪安全 Web安全 基础入门 - 第九天 信息打点 - CDN绕过篇&漏洞回链&接口探针&全网扫描&反向邮件

    一. CDN知识-工作原理及阻碍 (一)工作原理 1.概述:CDN的全称是Content Delivery Network,即内容分发网络.CDN是构建在现有网络基础之上的智能虚拟网络,依靠部署在各地 ...

  9. nodejs+koa+uniapp实现微信小程序登陆获取用户手机号及openId

    nodejs+koa+uniapp实现微信小程序登陆获取用户手机号及openId 前言: 我准备用nodejs+koa+uniapp实现一款餐饮点单小程序,以及nodejs+koa+vue实现后端管理 ...

  10. CF670A Holidays 题解

    Content 假设 \(1\) 年有 \(n\) 天,而每周同样会有 \(5\) 天工作日和 \(2\) 天休假.求一年最小的休假天数和最大休假天数. 数据范围:\(1\leqslant n\leq ...