HDU 1796 How many integers can you find
2015-09-27 11:52
267 查看
How many integers can you find
Time Limit: 5000msMemory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1796
64-bit integer IO format: %I64d Java class name: Main
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
Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(4)解题:容斥原理,注意有0
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 20; int a[maxn],n,m; LL LCM(LL a,LL b){ return a/__gcd(a,b)*b; } int main(){ while(~scanf("%d%d",&n,&m)){ int tot = m; for(int i = m = 0,tmp; i < tot; ++i){ scanf("%d",&tmp); if(tmp) a[m++] = tmp; } LL ret = 0; for(int i = 1; i < (1<<m); ++i){ LL lcm = 1; int cnt = 0; bool flag = false; for(int j = 0; j < m; ++j){ if((i>>j)&1){ cnt++; lcm = LCM(lcm,a[j]); if(lcm < 0){ flag = true; break; } } } if(flag) continue; if(cnt&1) ret += (n - 1)/lcm; else ret -= (n - 1)/lcm; } printf("%I64d\n",ret); } return 0; }
View Code
相关文章推荐
- opencv7-ml之统计模型
- 实现对一个8bit位数据(unsigned char类型)的指定位的置0或者置1操作
- Swift 学习笔记 - 02
- LeetCode Permutations
- 第三周vim入门学习1
- LAMP编译安装
- Auto Layout 界面自动布局系列 (使用代码添加布局约束)
- Ret2Libc 练习(2) -- VirtualProtect
- 关于JDBC连接Class.forName()的思考
- 多线程-经典买票问题
- Auto Layout界面自动布局系列 (使用VFL添加布局约束)
- Linux常用命令之文件处理命令
- 机器学习读书笔记(开篇)
- android改变字体的颜色的三种方法
- ASP.NET无刷新分页简单实现
- SetConsoleTextAttribute()用法
- redis key,string,hash,list,set,zset指令汇总
- Auto Layout界面自动布局 (Interface Builder布局)
- 【转】lucene4.3.0 配置与调试
- 第一章 软件工程概述