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>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
LL ans;
LL a[20], n, m;
int tot;
LL getgcd(LL x, LL y)
{
        return y==0?x:getgcd(y,x%y);
}
void dfs(int cur, int cnt, LL gcd)
{
        int i;
        if(cur==tot) return ;
        dfs(cur+1,cnt,1);
        gcd=a[cur]/getgcd(gcd,a[cur])*gcd;
        cnt++;
        if(cnt&1)
                ans+=n/gcd;
        else
                ans-=n/gcd;
        dfs(cur+1,cnt,gcd);
}
int main()
{
        int i;
        LL x;
        while(scanf("%I64d%I64d",&n,&m)!=EOF) {
                tot=0;
                for(i=0; i<m; i++) {
                        scanf("%I64d",&x);
                        if(x)
                                a[tot++]=x;
                }
                ans=0;
                n--;
                dfs(0,0,1);
                printf("%I64d\n",ans);
        }
        return 0;
}
时间: 2024-08-24 23:59:40

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

题意 就是给出一个整数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