[HDU 1796]How many integers can you find:容斥原理

学习提速专享!AI工具全家桶免费用 限时购周边加赠Coding Plan Lite,解锁20+主流AI工具,写代码、查资料快人一步! 阅读详情

点击这里查看原题

典型的容斥原理,枚举所有取数情况求最小公倍数
有几个坑需要注意:

  • 输入的数可能有0
  • 所有数的最小公倍数可能会爆int
/*
User:Small
Language:C++
Problem No.:1796
*/
#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
int n,m,p[15];
ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
int main(){
    freopen("data.in","r",stdin);//
    while(cin>>n>>m){
        int tot=0;
        for(int i=1;i<=m;i++)
            cin>>p[i];
        for(int i=1;i<=m;i++)
            if(p[i]) p[++tot]=p[i];
        m=tot;
        int ans=0;
        for(int i=1;i<(1<<m);i++){
            ll u=1,cnt=0;
            for(int j=0;j<m;j++){
                if((i>>j)&1){
                    cnt++;
                    u=lcm(u,p[j+1]);
                }
            }
            ans+=(n-1)/u*(cnt%2?1:-1);
        }
        cout<<ans<<endl;
    }
    return 0;
}
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): 7456    Accepted Submission(s): 2205 Problem Descripti 阅读详情

相关推荐

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): 6362    Accepted Submission(s): 1829 Problem Descripti

窦小雨的小点子 4205

hdu 1796 How many integers can you find[经典容斥原理]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1796 题目的意思:     让你

1558

[hdu 1796 How many integers can you find]容斥原理模板

hdu 1796 How many integers can you find 容斥原理模板 题目链接:hdu 1796 How many integers can you find  题意:给你一个数N,和有一个M个数的集合a1,a2,...,aM。0 分析:首先考虑特殊情况,N 当M==1,答案就是N / a1。 当M==2,答案就是N / a1 + N / a2 - N / l

Fighting! 492

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

题目链接: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): 5345   

David_Jett 1131

hdu1796 How many integers can you find容斥原理裸)

http://acm.hdu.edu.cn/showproblem.php?pid=1796 题意:求小于n的数中有多少个能被集合m中元素整除。 思路:挑战上的裸题,还基本是看的别人的思路,只学了一种深搜。就如书上所说我们利用的是m比较小的条件,容斥原理说白了就是被覆盖奇数次的个数加上,被覆盖偶数次的个数减去,以达到不重复计算的效果。 比如:集合m的元素有2、3、4、5

Iguodala的博客 505

HDU-1796 How many integers can you find

原题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1796 Sample Input 12 2 2 3 Sample Output 7 题目大意:给你两个整数n和m,m是一个整数集合的个数,我们所需要求的是1->n-1中有多少数可以被该整数集合某个元素整除。 解题思路:需要用到容斥定理。根据集合里的元素,遇奇则加,遇偶则减(其实就是羡慕别人一对对...

。。。的博客 210

HDU1796 - How many integers can you find容斥原理,GCD)

点击打开题目 How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6760    Accepted Submission(s): 1961 Pro

wyg1997 658

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

How many integers can you find Time Limit:5000MSMemory Limit:32768KB64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1796 Description Now you get a number N,...

apk6909的博客 155

hdu1796--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): 3132    Accepted Submission(s): 884 Problem Descriptio

ChenLI's blog 1098

How many integers can you find HDU - 1796

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 s

Grady_Ne的博客 447

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

题目大意:有一个序列,大小为m,里面有m个不超过20的非负数,各不相同。要求在1-n中有多少个能被m个数中任意一个数整除。 题目思路:简单的容斥原理应用。就不说了直接上代码。 有两种方法,一种是DFS,一种是直接位元素枚举暴力(study from zhixiaoli) DFS:(速度较快) #include #include using namespace std; long long

Jason 887

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

How many integers can you find Time Limit : 12000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 96   Accepted Submission(s) : 31 Font: Times New Roman |

Puya的博客 303

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

题目链接:点击查看 题目大意:给出一个n,再给出一个含有m个数的集合,问1~n-1中有多少个数可以被集合中的所有数字整除 题目分析:因为1~n-1中的每个数可能会被整除多次,所以我们可以利用容斥原理枚举集合,奇加偶减,从而就能算出正确的答案了,类似于之前计算有多少种互斥的数那样写一个dfs就好,不过需要修改一个细节,就是累乘的地方要写成求lcm,因为一个数若想被集合中的所有数整除,只需要被其l...

Falcon的博客 372

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

a709743744的博客 333

HDU1796 How many integers can you find 容斥原理

HDU1796 How many integers can you find 题意: 给出n,和m个数,求小于n,且可以被m个数中任意一个整除的数有哪些? 解题思路: 因为 m 很小,用二进制来枚举每种取法。 对于每种取法,求取出来的这些数的LCM,由容斥原理知道,选取的数的个数为奇数时加,偶数时减。 坑点在这 m 个非负数中有0 。 AC代码: #includ...

Hagtaril 306

How many integers can you findHDU-1796)(容斥原理

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

菜鸡的博客 220

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

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

__似水流年__的博客 253
上一篇: [HDU 4135]Co-prime:容斥原理
下一篇: [HDU 5710]Digit-Sum:其他
BrooksBUAA
博客等级 码龄10年 11粉丝 157原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值