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<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin
const LL  MOD = 1E9+7;
const LL N = 2e5+15;
const int maxn = 5e5+15;
const int letter = 130;
const int INF = 1e17;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,a[15];
LL gcd(LL a,LL b){
    if(b==0) return a;
    return gcd(b,a%b);
}
LL lcm(LL a,LL b){
    return a*b/gcd(a,b);
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=0;i<m;i++) {
            scanf("%d",a+i);
            if(!a[i])i--,m--;
        }
        n--;
        LL sum=0,ans;
        int num;
        for(int i=1;i<(1<<m);i++){
            ans=1;
            num=0;
            for(int j=0;j<m;j++){
                if((1<<j)&i){
                    num++;
                    ans=lcm(ans,1ll*a[j]);
                }
            }
            if(num%2) sum+=1ll*n/ans;
            else sum-=1ll*n/ans;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
时间: 2024-10-18 08:22:53

hdu1796 How many integers can you find(容斥)的相关文章

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 容斥第一题

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

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): 5493    Accepted Submission(s): 1567 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,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n+1) * n个数的最小公倍数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdl

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

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): 6001    Accepted Submission(s): 1722 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个数的集合. 问: [0, n-1] 中有多少个数 是集合中 某个数的倍数. 思路: 求的是有多少个数至少被集合中一个数整除=能被集合中一个数整除-被2个整除+被3个整除··· #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <

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

HDOJ1796 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): 6048    Accepted Submission(s): 1735 Problem Description Now you get a number N, and a M-integers set, you should