HDOJ1796 How many integers can you find(dfs+容斥)
2015-12-08 20:37
423 查看
How many integers can you find
Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6048 Accepted Submission(s): 1735
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
题目链接:点击打开链接
给出n, m, n代表1 - n的一个序列, 接下来m个数组成的集合, 问序列中可以整除任一集合中的一个数的个数和为多少.
对读入的m个数进行判断, 非0则赋值到a数组中, 进行dfs, dfs时进行容斥运算, id为奇数则加, 为偶数则减去重复的.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 15;
int n, m, num, ans, a[MAXN];
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
void dfs(int cur, int lcm, int id)
{
lcm = a[cur] / gcd(a[cur], lcm) * lcm;
if(id & 1) ans += (n - 1) / lcm;
else ans -= (n - 1) / lcm;
for(int i = cur + 1; i < num; ++i)
dfs(i, lcm, id + 1);
}
int main(int argc, char const *argv[])
{
while(scanf("%d%d", &n, &m) != EOF) {
num = ans = 0;
while(m--) {
int x;
scanf("%d", &x);
if(x != 0) a[num++] = x;
}
for(int i = 0; i < num; ++i)
dfs(i, a[i], 1);
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- POJ 2586 贪心
- DoTween - Sequence
- Unix 环境高级编程(APUE) 之 七 进程关系 和 守护进程
- 找出数组中的第K大的元素
- shell 脚本中将输出内容赋值给一个变量时的换行问题
- OpenJudge_P2229 Sumsets
- strstr
- MyEclipse中jquery.js文件报missing semicolon的错误解决
- Idea开发环境中搭建Maven并且使用Maven打包部署程序
- 马的走法
- 黑马程序员_JavaIO流(四)
- iOS笔试题03
- Python if else for while 语句控制 【整理】
- leetcode -- Implement strStr() -- KMP难理解,但考得不多,知道就好
- 深入浅出理解遗传算法
- 兔子问题
- 【转载】COM 组件设计与应用(十七)——持续性
- 【特征匹配】RANSAC算法原理与源码解析
- C++ 链表移动 类初始化
- 贪心算法解决背包问题