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): 6001    Accepted Submission(s): 1722

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

题解:wa

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>p;
typedef long long LL;
void rc(int x){
	LL sum=0;
	for(int i=1;i<(1<<p.size());i++){
		LL num=0,cur=1;
		for(int j=0;j<p.size();j++){
			if(i&(1<<j)){
				num++;
				cur*=p[j];
			}
		}
		if(num&1)sum+=x/cur-1;
		else sum-=x/cur-1;
	}
	printf("%lld\n",sum);
}
int main(){
	int N,M,x;
	while(~scanf("%d%d",&N,&M)){
		p.clear();
		for(int i=0;i<M;i++){
			scanf("%d",&x);
			if(x==0)continue;
			p.push_back(x);
		}
		rc(N);
	}
	return 0;
}

  

时间: 2024-10-10 20:36:36

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

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

hdu1796 How many integers can you find(容斥)

题目链接:点这里!!!! 题意:给你一个n(n<=2^31),m(m<=10),再给你m个数a[i](0<=a[i]<=20),问你[1,n-1]范围里有多少个数能被这m个数中的一个或多个整除. 题解:容斥裸体,注意a[i]=0的情况. sum = 被1个数整除的个数-被2个数整除的个数+被3个数整除的个数-.... (注意被多个数整除的时候,我们利用的是lcm) 代码: #include<cstdio> #include<cstring> #include

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 容斥入门

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 容斥(入门

题目链接:点击打开链接 题意: 给出常数n, m个数的集合. 问: [0, n-1] 中有多少个数 是集合中 某个数的倍数. 思路: 求的是有多少个数至少被集合中一个数整除=能被集合中一个数整除-被2个整除+被3个整除··· #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <

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

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