HDU 1796 How many integers can you find(简单容斥原理)

题目大意:有一个序列,大小为m,里面有m个不超过20的非负数,各不相同。要求在1-n中有多少个能被m个数中任意一个数整除。

题目思路:简单的容斥原理应用。就不说了直接上代码。

有两种方法,一种是DFS,一种是直接位元素枚举暴力(study from zhixiaoli)

DFS:(速度较快)

#include<iostream>
#include<algorithm>
using namespace std;
long long a[15];
int n, m;
int gcd(int a, int b)
{
	if (b == 0)
		return 0;
	int c = a%b;
	while (c)
	{
		a = b;
		b = c;
		c = a%b;
	}
	return b;
}
long long lcm(long long a, long long b)
{
	long long c = gcd(a, b);
	if (c == 0)
		return 0;
	return a*b / c;
}
int gao(int x)
{
	if (x == 0)
		return 0;
	return n / x;
}
int dfs(int i, bool flag, long long LCM)
{
	long long ans = 0;
	long long mid = lcm(max(LCM,a[i]), min(LCM, a[i]));
	if (flag) ans += gao(mid);
	else ans -= gao(mid);
	for (int j = i + 1; j <= m; j++)
		ans += dfs(j, !flag, mid);
	return ans;
}
int main()
{
	while (cin >> n >> m)
	{
		n--;
		for (int i = 1; i <= m; i++)
			cin >> a[i];
		long long num = 0;
		for (int i = 1; i <= m; i++)
			num += dfs(i, 1, 1);
		cout << num << endl;
	}
}

位元素枚举:(速度较慢)

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
int n, m;
ll a[15];
ll GCD(ll x, ll y)
{
	return y ? GCD(y, x%y) : x;
}
ll LCM(ll x, ll y)
{
	return x / GCD(x, y)*y;
}
ll gao(int l, int&cnt)
{
	ll res = 1;
	for (int i = cnt = 0; i < m; i++)
	{
		if (l&(1 << i))
		{
			cnt++;
			res = LCM(res, a[i]);
		}
	}
	return res;
}
int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		n--;
		int k=m;
		for (int i = 0; i < m; i++)
		{
			scanf("%lld", &a[i]);
			if (!a[i]) k = i;
		}
		if (k != m)
		{
			m--;
			swap(a[m], a[k]);
		}
		ll ans = 0;
		for (int i = 1; i < (1 << m); i++)
		{
			ll temp = gao(i, k);
			if (k & 1)
				ans += n / temp;
			else
				ans -= n / temp;
		}
		cout << ans << endl;
	}
}
时间: 2024-10-05 05:25:49

HDU 1796 How many integers can you find(简单容斥原理)的相关文章

HDU 1796 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): 5556    Accepted Submission(s): 1593 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): 4674    Accepted Submission(s): 1340 Problem Description Now you get a number N, and a M-integers set, you should

HDU 1796 How many integers can you find (状态压缩 + 容斥原理)

题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 } + sum{ 整除三个的数 }………………所以是奇加偶减,而整除 k 个数的数可以表示成 lcm(A1,A2,…,Ak) 的倍数的形式.所以算出最小公倍数, //HDU 1796 #include <cstdio> #include <iostream> #include <

HDU 1796 How many integers can you find(组合数学-容斥原理)

How many integers can you find 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

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

HDU 1796 How many integers can you find 容斥入门

How many integers can you find 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

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): 6710    Accepted Submission(s): 1946 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): 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): 5517    Accepted Submission(s): 1580 Problem Description Now you get a number N, and a M-integers set, you should