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

题目链接:点击打开链接

题意:

给出常数n, m个数的集合。

问:

[0, n-1] 中有多少个数 是集合中 某个数的倍数。

思路:

求的是有多少个数至少被集合中一个数整除=能被集合中一个数整除-被2个整除+被3个整除···

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <vector>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
const int inf = 1e8;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) { putchar('-');x = -x; }
    if(x>9) pt(x/10);
    putchar(x%10+'0');
}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 3e5+10;
int gcd(int x, int y){
	if(x > y)swap(x, y);
	while(x){
		y%=x;
		swap(x, y);
	}
	return y;
}
int lcm(int x, int y){
	return x/gcd(x, y) * y;
}
int n, m;
int a[N];
int ans;
vector<int>G[11];
void dfs(int x){
	if(x == m+1)return ;
	for(int i = 0; i < G[x].size(); i++)
	{
		int now = 1;
		for(int j = 0; j < m; j++)
			if(G[x][i]&(1<<j)) now = lcm(now, a[j]);
		now = n/now;
		if(x&1)ans += now;
		else ans -= now;
//	printf("%d:(%d,%d) %d\n",G[x][i], x, ans, now);
	}
	dfs(x+1);
}
int main(){
	while(cin>>n>>m){
		n--;

		for(int i = 0; i < m; i++){
			rd(a[i]);
			if(!a[i])i--,m--;
		}
		if(!m){puts("0");continue;}
		for(int i = 0; i <= m; i++)G[i].clear();
		for(int i = 0; i < (1<<m); i++){
			int cnt = 0, siz = i;
			while(siz){
				cnt++; siz &= siz-1;
			}
			G[cnt].push_back(i);
		}
		ans = 0;
		dfs(1);
		pt(ans); puts("");
	}
    return 0;
}
时间: 2024-10-08 10:17:06

HDU 1796 How many integers can you find 容斥(入门的相关文章

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 (容斥)

题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n+1) * n个数的最小公倍数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdl

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(容斥原理+二进制/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