hdu1796 How many integers can you find 容斥原理

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 

能被给出的第二行的数整除的数的个数;

sum=被一个整除-被两个整除+被三个整除-。。。。。。

注意一个他自己本身不算

#include<cstdio>

#include<cmath>

#include<cstring>

#include<iostream>

#include<algorithm>

#include<queue>

#include<vector>

#include<map>

#include<stack>

#pragma comment(linker,"/STACK:102400000,102400000")

#define pi acos(-1.0)

#define EPS 1e-6

#define INF (1<<24)

using namespace std;

int m,n,cnt;

int num[20];

int visi[20];

long long int sum;

long long int gcd(long long int a,long long int b) //最大公约数

{

if(b==0)

{

return a;

}

return gcd(b,a%b);

}

long long int lcm(long long int a,long long int b) //最小公倍数

{

return a/gcd(a,b)*b;

}

void solve()

{

int i,flag=0;//记录有多少个数

long long int t=1,ans;

for(i=0;i<cnt;i++)

{

if(visi[i])

{

flag++;

t=lcm(t,num[i]);

}

}

ans=n/t;

if(n%t==0) ans--;

if(flag%2==1) sum=sum+ans; //奇数加,偶数减

else sum=sum-ans;

}

int main()

{

while(scanf("%d %d",&n,&m)!=EOF)

{

int i,j;

int a;

cnt=0;

sum=0;

for(i=0;i<m;i++)

{

scanf("%d",&a);

if(a!=0) num[cnt++]=a;

}

//int m=cnt;

int zhuang=1<<cnt;

for(i=1;i<zhuang;i++) //状态

{

int tem=i;

for(j=0;j<cnt;j++) //状态取数情况

{

visi[j]=tem&1;

tem=tem>>1;

}

solve();

}

printf("%I64d\n",sum);

}

return 0;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 00:20:59

hdu1796 How many integers can you find 容斥原理的相关文章

[ACM] 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(组合数学、dfs)

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

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

这个题的m的数中居然有0啊,RE了好几次.... 初学容斥原理,这才知道还有奇加偶减这个东西,以前一直以为容斥原理不过是把重复的删掉就好了,.. 然后知道奇加偶减这个东西后,就可以深搜了,将所有组合情况全列出来,然后求lcm就好了.数的个数就是(n-1)/lcm,虽然我代码里写的是gcd..不要在意这些细节... #include <iostream> #include <string.h> #include <math.h> #include <queue>

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

题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有多少m中元素的个数,在结合LCM考虑容斥. 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath> 5 #include<algorithm> 6 u

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. 思路:

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 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 (状态压缩 + 容斥原理)

题目链接 题意 : 给你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(容斥原理+二进制/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