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. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
随机推荐
- (转载)java排序实现
Java实现几种常见排序方法 日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序,甚至还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等. 冒泡排序是一种简单的排序算法. ...
- 23. 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
原文:https://blog.csdn.net/u011596455/article/details/60322568 版权声明:本文为博主原创文章,转载请附上博文链接! 在Ubuntu中,有时候运 ...
- Hadoop RPC通信
Remote Procedure Call(简称RPC):远程过程调用协议 1. 通过网络从远程计算机程序上请求服务 2. 不需要了解底层网络技术的协议(假定某些传输协议的存在,如TCP或UDP) 3 ...
- Xcode功能快捷键
隐藏xcode command+h退出xcode command+q关闭窗口 command+w关闭所有窗口 command+option+w关闭当前项目 command+control+w关闭当前文 ...
- OpenStack之六: plancement服务(端口8778)
官网地址:https://docs.openstack.org/placement/stein/install/install-rdo.html #:创建placement库,并授权 MariaDB ...
- Java面试基础--(出现次数最多的字符串)
题目:给定字符串,求出现次数最多的那个字母及次数,如有多个 重复则都输出. eg,String data ="aaavzadfsdfsdhshdWashfasdf": 思路: 1. ...
- Centos 7 安装redis,修改配置文件不生效、外网不能访问。
前提: 在用Centos 7 安装 redis 时,遇上一下几个问题 ,记录下 . 1.修改配置文件,按官网步骤启动,不生效. 2.外网无法访问redis. 步骤: 1.打开centos 虚拟机 ,按 ...
- 『学了就忘』Linux启动引导与修复 — 74、Linux系统的修复模式(光盘修复模式)
目录 1.光盘修复模式概念 2.光盘修复模式修复系统问题 (1)准备系统光盘 (2)进入BIOS (3)修改BIOS的启动顺序 (4)进入光盘修复模式 (5)修复系统 (6)修复系统实操 (7)总结 ...
- Nginx模块之Nginx-echo
目录 一.简介 二.使用 一.简介 官网 Nginx-echo可以在Nginx中用来输出一些信息,是在测试排错过程中一个比较好的工具.它也可以做到把来自不同链接地址的信息进行一个汇总输出.总之能用起来 ...
- Python3元组的简介和遍历
一.Python3元组简介 1.1.如何创建一个元组 ''' Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号(),列表使用方括号[]. 元组创建很简单,只需要在括号中添 ...