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

数字IC后端手把手实战教程 | Innovus verify_drc VIA1 DRC Violation解析及脚本自动化修复方案 数字IC后端实战项目案例之Innovus verify_drc DRC Violation脚本自动化修复方案 阅读详情

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;
}
【GameFramework框架内置模块】15、场景(Scene) 场景(Scene)模块,提供场景管理的功能,负责场景的加载、卸载、切换和管理。可以方便地管理不同场景之间的切换以及不同场景之间的信息传递。 阅读详情

相关推荐

【Labview】对连续采集数据的一种采样方法

图1如图1,想要对这样一个连续信号进行数据点的采集,我根据网上收集到的方法,在此分享一下最终的结果。可以拓展结合文件的写入,完成对连续信号的采集和输出。 已知: 一个连续信号,如图1. 目标: 设定一定采集点个数、采集点时间间隔、和采集周期次数,得到这条连续信号的数据点,如图2. 图2 方法如下: 主要利用的是队列的方法。 在连续信号生成的时候,不断的写入队列,并且清除队列。 ...

李时珍食疗志的博客 1万+

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

应用处理器(AP)、系统级芯片(SoC)与微控制器(MCU)的区别与联系

应用处理器(AP)、系统级芯片(SoC)与微控制器(MCU)的区别与联系

a_zxswer的博客 1722

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 493

从公式到实践:用SPSS解锁香农多样性指数的生态洞察

本文详细介绍了如何利用SPSS软件计算和应用香农多样性指数进行生态研究。从基础公式解析到SPSS实战操作,再到生态决策支持,全面展示了该指数在物种丰富度和均匀度量化中的价值。文章特别强调了SPSS操作中的关键技巧和常见误区,为生态研究者提供了一套完整的分析解决方案。

weixin_30832143的博客 478

四、zabbix7.0推送告警至钉钉webhook机器人

2、钉钉的webhook是有安全要求的,我采用的是ip的方式,我的zabbix服务器在内网,我的网络出口有多个固定公网ip,所以这样做省事,其他方式也可以你自己考虑。下图是webhook机器人的配置。这一步,参数里面默认的跟我下图里不一样的,按我下图修改。Key是你的webhook机器人地址里的access_token=后面的内容。整个逻辑就是告警动作匹配告警媒介与用户,然后进行告警的发送。1、zabbix服务器能够访问钉钉的服务器,具体说是能访问。二、zabbix7配置报警媒介以及消息模板配置。

m0_74823715的博客 1309

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

【Azure 架构师学习笔记】- Azure Databricks (1) - 环境搭建

Databricks 已经成为了数据科学的必备工具,今时今日你已经很难抛开它来谈大数据,它常用于做复杂的ETL中的T, 数据分析,数据挖掘等,特别适用于做数据建模,机器学习等。那么顺应时代,现在也来看看这个工具的内容。首先要有一个环境,基于Azure 的Databricks简称ADB。托管在Azure 上的Databricks已经被Azure进行了很大的优化, 在搭建时只需要简单的几步即可拥有一个环境,不过要提醒一句ADB的集群并不便宜,用完马上删掉或停止, 否则一晚过百美金就会烧掉。

MVP黄钊吉(發糞塗牆) 2312

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_的博客 452

搞定汽车座椅电机EMC超标:一个PCB滤波板的实战设计与避坑心得

本文详细介绍了汽车座椅电机EMC超标问题的解决方案,通过PCB滤波板的实战设计,解决了传导发射测试超标问题。文章分享了在空间受限条件下的滤波电路架构设计、接地系统优化及迭代验证过程,为汽车直流电机的EMC整改提供了实用方法论。

weixin_30336061的博客 390

Zemax光机热集成分析:从FEA数据导入到光学性能评估实战

在精密光学系统设计中,环境温度变化和机械应力是导致像质恶化的关键因素,这涉及到光、机、热多物理场的耦合分析。其核心原理在于,通过有限元分析(FEA)软件(如ANSYS)模拟热与力学载荷下的结构变形和温度分布,并将这些物理场数据精准映射到光学设计软件(如Zemax)中,以评估其对调制传递函数(MTF)、点列图等核心光学指标的影响。这项技术的工程价值在于,它使设计从静态的理想模型跃升至动态的工程现实,能够在设计阶段就预测并补偿环境扰动,从而显著提升光学系统在航空航天、车载镜头、高功率激光等严苛工况下的可靠性与稳

weixin_30565327的博客 368

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 891

VMware Workstation 17 Pro 保姆级教程:手把手教你安装银河麒麟 Kylin V10 SP3 国产系统

本文提供VMware Workstation 17 Pro安装银河麒麟Kylin V10 SP3的详细教程,涵盖环境准备、虚拟机配置、系统安装及优化全流程。通过分步指导和实用技巧,帮助用户高效搭建国产操作系统开发测试环境,特别适合技术自主可控需求场景下的虚拟化实践。

weixin_30879169的博客 653

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

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

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

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

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

1567

数学建模模型总结

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

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

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

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的专栏 522

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

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、付费专栏及课程。

余额充值
⌂ 换网址