HDOJ1796 How many integers can you find(dfs+容斥)

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;
}
时间: 2024-08-08 01:27:30

HDOJ1796 How many integers can you find(dfs+容斥)的相关文章

HDU 1796 How many integers can you find (lcm + 容斥)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5526    Accepted Submission(s): 1584 Problem Description Now you get a number N, and a M-integers set, you should

【BZOJ1853/2393】[Scoi2010]幸运数字/Cirno的完美算数教室 DFS+容斥

[BZOJ1853][Scoi2010]幸运数字 Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”总是太少了,比如在[1,100]的区间内就只有6个(6,8,66,68,86,88),于是他又定义了一种“近似幸运号码”.lxhgww规定,凡是“幸运号码”的倍数都是“近似幸运号码”,当然,任何的“幸运号码”也都是“近似幸运号码”

广州工业大学2016校赛 F 我是好人4 dfs+容斥

Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍数 最后友情提供解题代码(我真是太好人了) void solve(int p[], int n) { int ans = 0; for (int i = 1; i <= 1e9; i++) { int fl = 0; for (int j = 0; j < n; j++) { if (i % p[

P2567 [SCOI2010]幸运数字 DFS+容斥定理

P2567 [SCOI2010]幸运数字 题目描述 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是"幸运号码"!但是这种"幸运号码"总是太少了,比如在[1,100]的区间内就只有6个(6,8,66,68,86,88),于是他又定义了一种"近似幸运号码".lxhgww规定,凡是"幸运号码"的倍数

HDU1796 How many integers can you find【容斥定理】

题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包括零),求小于N的 正整数(1~N-1)中,能被M个整数的集合中随意一个元素整除的正整数个数. 比如N = 12.M = {2,3},在1~N-1中,能被2整除的数为{2,4,6.8.10},能被3整除的数为 {3.6,9}.则所求集合为{2,3,4.6,8,9,10},共7个,则答案为7. 思路:

HDU 1796 How many integers can you find 【容斥】

<题目链接> 题目大意: 给你m个数,其中可能含有0,问有多少小于n的正数能整除这个m个数中的某一个. 解题分析: 容斥水题,直接对这m个数(除0以外)及其组合的倍数在[1,n)中的个数即可,因为可能会重复计算,所以在叠加的时候进行容斥处理,下面用的是位运算实现容斥. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 us

hdu 1796 How many integers can you find 容斥定理

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) 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 t

HDU 1796 How many integers can you find (容斥定理 + 二进制)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5493    Accepted Submission(s): 1567 Problem Description Now you get a number N, and a M-integers set, you should

hdu 1796 How many integers can you find 容斥第一题

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6710    Accepted Submission(s): 1946 Problem Description Now you get a number N, and a M-integers set, you should