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

Tartancalib标定OAK-FFC_4p(上) 1.使用Tartancalib标定双目或多目相机前,先对单个相机标定获得内参,再获得外参。 阅读详情

How 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 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. 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
题意:

给定n和一个大小为m的集合,集合元素为非负整数。为1…n内能被集合里任意一个数整除的数字个数。n<=2^31,m<=10

分析:

首先我们将能被i整除记为满足性质AiAi,那么根据题意实际是让我们求

|A1⋃A2⋃⋯⋃Am||A1⋃A2⋃⋯⋃Am|

根据容斥原理的推论:

|A1⋃A2⋃⋯⋃Am||A1⋃A2⋃⋯⋃Am|

=∑|Ai|−∑|Ai⋃Aj|+∑|Ai⋃Aj⋃Ak|+⋯=∑|Ai|−∑|Ai⋃Aj|+∑|Ai⋃Aj⋃Ak|+⋯

                                +(−1)m+1|A1⋃A2⋃⋯⋃Am|                                +(−1)m+1|A1⋃A2⋃⋯⋃Am|

根据组合数我们知道它的项数一共有:

C0m+C1m+C2m+⋯+Cmm=2mCm0+Cm1+Cm2+⋯+Cmm=2m

因为m不超过10,所以完全可以枚举完成

巧妙利用二进制表示取法1代表取0表示不取,然后用按位与得到选取了哪一个

满足奇数个性质是加号,满足偶数个性质是减号

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,a[20];
ll gcd(ll a,ll b){
    return b ? gcd(b,a%b) : a;
}
int main(){
    while(scanf("%lld%lld",&n,&m) != EOF){
        int ans = 0;
        vector<int> v;
        for(int i = 0; i < m; i++){
            scanf("%lld",&a[i]);
            if(a[i] > 0) v.push_back(a[i]);//注意不能有0
        }
        m = v.size();//更新长度
        for(int i = 1; i < (1 << m); i++){//注意这里其实枚举到(1<<m)-1即m个1表示m个性质全有
            int cnt = 0;
            ll x = 1;
            for(int t = 0; t < m; t++){//枚举每个性质看当前情况选择了那个性质
                if(i & (1 << t)){
                    cnt++;
                    x = x * v[t] / gcd(x,v[t]);
                }
            }
            if(cnt & 1) ans += (n - 1) / x;
            else ans -= (n - 1) / x;
        }
        printf("%d\n",ans);
    }
    return 0;
}
华为P812E光猫补全shell配置PON口模式 终于看到华为企业级的带2.5电口P812E横空出世了,相信很多发烧友也已经将此收入昂中。相信体验的同时,遇到的第一件事情就是配置PON口模式的问题,本文将带发烧基友解锁模式。 阅读详情

相关推荐

AXI backpressure反压机制代码

本文将xilinx的axi读写代码进行修改,实现吞吐量达到50%

m0_64636251的博客 1773

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

wait 285

提升RAG效果:为何 JSON 格式远胜 Markdown?

在构建强大的 RAG (检索增强生成) 系统时,文档解析是至关重要的第一步。它直接影响着后续的检索效率和生成质量。在众多文档格式中,JSON (JavaScript Object Notation) 格式正逐渐展现出其相对于传统 Markdown 格式的巨大优势。本文将深入探讨 JSON 在 RAG 系统文档解析中的卓越之处,并结合具体案例,让你理解为何 JSON 才是构建下一代智能应用的更优选择。RAG 系统的核心目标是让模型能够利用外部知识库,生成更精准、更贴合实际的答案。

simoncool23的博客 2727

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

booty 491

Tessent IJTAG ICL:从规范到实践的硬件描述语言指南

本文深入解析Tessent IJTAG ICL语言在硬件测试中的应用,从规范到实践提供全面指南。通过模块化设计、网络提取与编辑等实战技巧,帮助工程师高效构建标准化测试网络,显著提升芯片测试效率与覆盖率。特别分享TDR/SIB/TAP的ICL实现方法及调试策略,适用于SoC、车载芯片等复杂场景。

weixin_33606346的博客 307

PCL 基于强度的双边滤波【2026最新版】

基于强度信息的双边滤波代码实现、滤波前后点云未出现变化的原因以及获取带有强度pcd点云的方法。本文由CSDN点云侠原创,博客长期更新,本文最近一次更新时间为:2026年5月24日。

点云侠的博客 29万+

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

菜鸡的博客 223

【推荐系统】推荐算法:冷启动-召回-粗排-精排-重排 解读

【推荐系统】推荐算法:冷启动-召回-粗排-精排-重排 解读

Danger的博客 3763

Can you find it? 二分

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.  InputThe

Cold_Sun_的博客 451

顶级AI大模型白嫖ChatGPT Plus1个月,0成本薅顶配功能

Chat*** Plus限时免费1个月!原价20美元/月的顶配功能

BUG猿的博客 7345

以阿克曼转向车为例对URDF和XACRO文件进行解读

   最近我在古月学院购买了在Gazebo中实现阿克曼转向车的仿真课程,课程资料的阿克曼小车的XACRO文件在Gazebo里加载的时候报了三个错误,在这之前,我没接触过URDF或XACRO文件,本篇文章就以这个阿克曼小车的XACRO文件为例,对URDF或XACRO文件的格式和内容进行解读,期望经过解读和学习,找到错误原因及所在,并解决这三个错误    先来放一下上面右图所示的模型的完整的XACRO文件: <?xml version="1.0"?> <!-- racecar.urdf.x

慕羽★的博客 3704

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 890

AutoClip AI视频剪辑工具:Docker一键部署指南

摘要:AutoClip是一个基于Docker容器化部署的智能视频剪辑系统,采用微服务架构设计,包含Web界面、后端API、异步任务处理和Redis缓存等组件。系统支持一键快速部署,提供完整的监控和日志功能,最低要求4GB内存和10GB存储空间。部署过程仅需三个步骤:克隆代码、配置环境变量、启动容器服务。生产环境支持性能调优、数据持久化和网络安全管理。该系统解决了环境配置难题,实现"一次配置,到处运行"的目标,适合需要自动化视频处理的用户。

wjianwei666的专栏 1017

hdoj 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): 5331    Accepted Submission(s): 1513 Problem Descripti

世界很大 903

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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 1134

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

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

1566

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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的博客 304

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

HDOJ 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): 4852    Accepted Submission(s): 1389 Problem Descripti

码代码的猿猿 884

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的博客 334

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

传送门:hdu 1796 How many integers can you find题目大意在M个集合中,找到能被N整除(不包含N)的个数解题思路首先要解决这个题目要知道什么容斥定理! 我们先假设N=100,然后M=3其中M集合中的元素为{1,2,3} 如果我们直接计算N/a1 = 100,N/a2=50,N/a3 = 33,在计算前两个的时候,N/a2这么多个元素就是多余的,在计算后面两个的

woshinannan的专栏 521

hdu1796 How many integers can you find 容斥原理

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

xtulollipop 652

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的博客 156

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 309

C - How many integers can you find(容斥原理&暴力)

C - How many integers can you find(容斥原理&暴力) 思路:暴力或者搜索,因为MMM最大只有10,根据容斥原理可以用选出的元素个数计算对答案的贡献,奇数就加,偶数就减。 AC代码: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<string> #include<cmath>

Harris-H的博客 550

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

题意:       求1->n-1之间能被一个集合A内元素整除的数的个数,例如n = 12, A = {2, 3} 则能被A集合元素整除的数的集合为{2,  3, 4 , 6, 8, 9, 10}则结果为7。 解法:容斥定理,用回溯算法。 让我没想到的是用深搜来解决多重for循环问题 Description   Now you get

u013087645的专栏 709
上一篇: String painter HDU - 2476(区间DP)
下一篇: oval-and-rectangle HDU - 6362(求定积分+化简)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值