How many integers can you find(hdu1796)
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
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.
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.
2 3
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)的更多相关文章
- 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 ...
- 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 ...
- HDU1796 How many integers can you find(容斥原理)
题目给一个数字集合,问有多少个小于n的正整数能被集合里至少一个元素整除. 当然是容斥原理来计数了,计算1个元素组合的有几个减去2个元素组合的LCM有几个加上3个元素组合的LCM有几个.注意是LCM. ...
- hdu1796 How many integers can you find
//设置m,Q小于n可以设置如何几号m随机多项整除 //利用已知的容斥原理 //ans = 数是由数的数目整除 - 数为整除的两个数的数的最小公倍数 + 由三个数字... #include<cs ...
- HDU1796 How many integers can you find【容斥定理】
题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包 ...
- [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 ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
随机推荐
- STM32 BootLoader升级固件
一.知识点 1.BootLoader就是单片机启动时候运行的一段小程序,这段程序负责单片机固件的更新,也就是单片机选择性的自己给自己下程序.可以更新,也可以不更新,更新的话,BootLoader更新完 ...
- 9 — springboot整合jdbc、druid、druid实现日志监控 — 更新完毕
1.整合jdbc.druid 1).导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...
- nodeJs-Stream接口
JavaScript 标准参考教程(alpha) 草稿二:Node.js Stream接口 GitHub TOP Stream接口 来自<JavaScript 标准参考教程(alpha)> ...
- 字节面试问我如何高效设计一个LRU,当场懵
首发公众号:bigsai 转载请放置作者和原文(本文)链接 前言 大家好,我是bigsai,好久不见,甚是想念! 最近有个小伙伴跟我诉苦,说他没面到LRU,他说他很久前知道有被问过LRU的但是心想自己 ...
- c学习 - 第五章:选择结构程序设计
5.2 关系运算符与逻辑运算符 !(非) ^ 高 算术运算符 | 关系运算符 | &&和 || | 赋值运算符 | 低
- Vue API 3模板语法 ,指令
条件# v-if# v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回 truthy 值的时候被渲染. v-show# v-show 指令也是用于根据条件展示一块内容.v-show ...
- my36_InnoDB关键特性之change buffer
一.关于IOT:索引组织表 表在存储的时候按照主键排序进行存储,同时在主键上建立一棵树,这样就形成了一个索引组织表,一个表的存储方式以索引的方式来组织存储的. 所以,MySQL表一定要加上主键,通过主 ...
- class.getName()和class.getSimpleName()的区别
根据API中的定义: Class.getName():以String的形式,返回Class对象的"实体"名称: Class.getSimpleName():获取源代码中给出的&qu ...
- linux查询健康状态,如何直观的判断你的Linux系统是否健康
一提到对于查看系统运行的健康状况,可能大多数朋友考虑到的就是查看进程或者打开任务管理器,但是对于应用在真实生产环境中服务器的linux系统来说,以上两种方式都不是***效的查看方式,那么今天就给大家推 ...
- @ResponseBody和@RequestBody
@ResponseBody @ResponseBody的作用其实是将java对象转为json格式的数据. @responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转 ...