HDU 1796

呃,我竟然傻了,同时被a且b整除的个数为n/(a*b)。

其实应该是n/[a,b]才对,是他们的最小公倍数啊。。。

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

__int64 ans;
__int64 set[30];
__int64 n;
int m;

__int64 gcd(__int64 a,__int64 b){
	if(b==0) return a;
	return gcd(b,a%b);
}

void dfs(int i,int num,__int64 tmp){
	if(i>=m){
		if(num==0){
			ans=0;
		}
		else {
			if(num&1){
				ans=(ans+n/tmp);
			}
			else{
				ans=ans-n/tmp;
			}
		}
		return ;
	}
	dfs(i+1,num,tmp);
	dfs(i+1,num+1,tmp*set[i]/gcd(tmp,set[i]));
}

int main(){
	bool flag;
	while(scanf("%I64d%d",&n,&m)!=EOF){
		n--;
		flag=true;
		int k=0;
		for(int i=0;i<m;i++){
			scanf("%I64d",&set[i]);
			if(set[i])
			set[k++]=set[i];
		}
		m=k;
		ans=0;
		dfs(0,0,1);
		printf("%I64d\n",ans);
	}
	return 0;
}

  

时间: 2024-11-02 12:25:09

HDU 1796的相关文章

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 4135,Hdu 1796)

题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1412    Accepted Submission(s): 531 Problem Description Given a number N, you are asked to count the number of integers betwe

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 Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7439    Accepted Submission(s): 2200 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) 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(容斥原理+二进制/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

题意: 给一个N,然后给M个数,问1~N-1里面有多少个数能被这M个数中一个或多个数整除. 思路: 首先要N-- 然后对于每个数M 其实1~N-1内能被其整除的 就是有(N-1)/M[i]个 但是会出现重复 比如 样例 6就会被重复算 这时候我们就需要容斥原理了 加上一个数的减去两个数的.. 这里要注意了 两个数以上的时候 是求LCM而不是简单的相乘! 代码: #include "stdio.h" #include "string.h" #include "

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