[容斥原理] 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 "math.h"
#include "iostream"
#include "cstdlib"
#include "algorithm"
#include "queue"
using namespace std;
int a[12];
int used[12],b[12];
int n,m;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int lcm(int k)
{
    int ans=b[0];
    for(int i=1;i<k;i++)
    {
        int tep=gcd(ans,b[i]);
        ans=ans/tep*b[i];
    }
    return ans;
}
__int64 dfs(int kk,int x,int lit)
{
    __int64 ans=0;
    if(x==lit)
    {
        int tep;
        tep=lcm(x);
        return n/tep;
    }
    for(int i=kk+1;i<m;i++)
    {
        if(a[i]==0) continue;
        if(used[i]) continue;
        used[i]=1;
        b[x]=a[i];
        ans+=dfs(i,x+1,lit);
        used[i]=0;
    }
    return ans;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=-1)
    {
        n--;
        for(int i=0;i<m;i++) scanf("%d",&a[i]);
        __int64 ans=0;
        for(int i=1;i<=m;i++)
        {
           // printf("%d\n",dfs(-1,0,i));
            memset(used,0,sizeof(used));
            if(i%2==0) ans-=dfs(-1,0,i);
            else ans+=dfs(-1,0,i);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-10-11 14:05:00

[容斥原理] hdu 1796 How many integers can you find的相关文章

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

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

这个题的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(简单容斥原理)

题目大意:有一个序列,大小为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(i

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

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