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. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
随机推荐
- python飞机大战
'''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...
- Webpack 打包 Javascript 详细介绍
本篇我们主要介绍Webpack打包 Javascript.当然,除了可以打包Javascript之外,webpack还可以打包html.但是这不是我们本篇的重点.我们可以参考 Webpack HTML ...
- 【leetcode】917. Reverse Only Letters(双指针)
Given a string s, reverse the string according to the following rules: All the characters that are n ...
- 【Linux】【Services】【Disks】bftfs
1. 简介 1.1 Btrfs(B-tree,Butter FS,Better FS) 1.2. 遵循GPL,由oracle在2007年研发,支持CoW 1.3. 主要为了替代早期的ext3/ext4 ...
- Ajax请求($.ajax()为例)中data属性传参数的形式
首先定义一个form表单: <form id="login" > <input name="user" type="text&quo ...
- 使用Booststrap布局网页页面
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="utf-8&qu ...
- Nginx编译添加新模块
目录 一.简介与思路 一.简介与思路 当前适用于nginx已经在安装过了,如果没安装过,直接在编译时候添加模块即可. Nginx主要程序就是nginx这个二进制脚本,只要在编译一个nginx脚本替换掉 ...
- 09 - Vue3 UI Framework - Table 组件
接下来做个自定义的表格组件,即 table 组件 返回阅读列表点击 这里 需求分析 开始之前我们先做一个简单的需求分析 基于原生 table 标签的强语义 允许用户自定义表头.表体 可选是否具有边框 ...
- Office365与Office2016差异汇总
以下很多链接来自原来的博客,如果有哪篇"被色情"的,请留言联系我,谢谢! 2020-8-29更新 通用 图片透明度:http://blog.sina.com.cn/s/blog_5 ...
- WPF之交互触发器(CallMethodAction)学习
需求背景: 当我们需要制作画板时,我们的VM需要记录我们的坐标并保存到Path的Data中,用我们普通的Command是无法办到的,这时我们就衍生出来了一个交互触发器CallMethodAction ...