[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 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

Author

wangye

Source

2008
“Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(4)

到现在才学习容斥....以前见到过好多次这个题,都没认真做....

给定一个包括m个数的集合,数不超过20,问有多少个<n的数,能够被集合中的数整除。

关键问题是重复计算,比如6可以被2整除,也可以被3整除,计数了两次,所以要用到容斥原理。

容斥就是对于重叠次数只有奇数次的,我们加上,重叠次数为偶数次的,我们要减去。

对于每个数,都计算重叠一次的,重叠两次的,重叠三次的...最多重叠m次,有m个数

所以要用到DFS,然后保存中间态重叠x次的最小公倍数lcm,符合题意的数有(n-1)/lcm个

(**  1~n之间有多少个能被x整除的数,公式为n/x,题目中要求小于n,所以(n-1)/x  **),根据

step重叠的次数或者加上,或者减去。

比如n=7,m=2,集合中的数为2 3

首先对于2,重叠一次,lcm=2,7/2=3,有3个符合的数(其实是2,4,6),因为重叠次数是奇数,所以

ans+=3,这时中间态lcm=2,然后继续dfs剩下的数(因为要和其它数组和在一块进行重叠,这里只剩下3了),

重叠两次, lcm=LCM(lcm,3), lcm=LCM(2,3)=6,7/6=1,重叠次数为偶数,ans-=1,这样以2开始的所有情况就完了。

再对于3,重叠一次,lcm=3,7/3=2,有两个符合的数,ans+=2.最终ans=4

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#define rd(x) scanf("%d",&x)
#define rd2(x,y)  scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=25;
ll num[maxn];//寸有效数字
int cnt;
ll ans;
ll n,m;

ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
ll LCM(ll a,ll b)
{
    return a*b/gcd(a,b);
}

void dfs(int th,ll now,int step)//th,该数是num中的第几个,now是当前的最小公倍数,step是重叠几次,最多cnt次
{
    if(step>cnt)
        return;
    ll lcm=LCM(num[th],now);//这里lcm是step个数的最小公倍数
    if(step&1)
        ans+=(n-1)/lcm;//1~n之间有多少个能被x整除的数,公式为n/x,题目中要求小于n,所以(n-1)/x
    else
        ans-=(n-1)/lcm;
    for(int p=th+1;p<cnt;p++)
        dfs(p,lcm,step+1);
}

int main()
{
    while(scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        cnt=0;
        ans=0;
        ll val;
        while(m--)
        {
            scanf("%I64d",&val);
            if(val>0&&val<n)
                num[cnt++]=val;
        }
        for(int i=0;i<cnt;i++)//从每个数开始,搜索重叠一次,两次,三次....
        {
            dfs(i,num[i],1);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-10-11 03:48:11

[ACM] HDU 1796 How many integers can you find (容斥原理)的相关文章

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

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

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(容斥原理+二进制/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): 6710    Accepted Submission(s): 1946 Problem Description Now you get a number N, and a M-integers set, you should