How many integers can you find HDU - 1796

【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘 虚拟化平台安装好之后,一般情况下,只有一个硬盘在工作,其他的外接硬盘(我的只有一个ssd硬盘,其他的均是usb外接,SATA接口的硬盘是否这样我暂时不清楚哈)没有在工作,但是一个硬盘明显不够我们使用的,所以我们需要去挂载外接的硬盘来扩容。... 阅读详情
 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. 0<N<2^31,0<M<=10, 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个元素的集合,求小于n的数中是m中某个元素的倍数有多少个。

题目分析:这是一道简单的容斥原理的题目,下面根据这道题给出容斥原理的三种实现方法: dfs,队列数组,二进制;

注意:这道题求的是小于n的数中有多少符合条件的,不包含n本身;

二进制实现:

#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
vector<int>x;
int gcd(int a,int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int solve(int n)
{
     int ans=0;
    for(int i=1;i<(1<<x.size());i++)
    {
        int lcm=1,cnt=0;
        for(int j=0;j<x.size();j++)
          if(i&(1<<j))
          {
              cnt++;
              int g=gcd(lcm,x[j]);
              lcm=lcm/g*x[j];
          }
          if(cnt&1)
            ans+=n/lcm;
          else
            ans-=n/lcm;
    }
    return ans;
}
int main()
{
    int n,m;
    int temp;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        n--;
        x.clear();
        for(int i=1;i<=m;i++)
        {
           scanf("%d",&temp);
           if(temp)  ///不能为0
           x.push_back(temp);
        }
        cout<<solve(n)<<endl;
    }
    return 0;
}

深搜的两种实现:


1.用数组存储元素

#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
int get_lcm(int a,int b)
{
    int x=a;
    int y=b;
    while(b)
    {
        int t=a;
        a=b;
        b=t%b;
    }
    return x/a*y;
}

long long ans;
int b[20],k,n;
void dfs(int i,int cnt,int lcm)///i代表数组的下标
{                              ///cnt代表几个元素的最小公倍数   
    lcm=get_lcm(lcm,b[i]);      ///lcm代表最小公倍数
    if(cnt&1)
        ans+=n/lcm;
    else
        ans-=n/lcm;
    for(int j=i+1;j<k;j++)
        dfs(j,cnt+1,lcm);
}
int main()
{
    int m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        n--;
        ans=k=0;
        for(int i=1;i<=m;i++)///k记录非0的个数
        {
            scanf("%d",b+k);///给这个数组赋的值不能为0;
            if(!b[k++]) k--;///这种赋值方法有趣
        }
        for(int i=0;i<k;i++)
            dfs(i,1,1);
        cout<<ans<<endl;
    }
    return 0;
}

2.用vector实现

#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
int get_lcm(int a,int b)///获得最小公倍数
{
    int x=a;
    int y=b;
    while(b)
    {
        int t=a;
        a=b;
        b=t%b;
    }
    return x/a*y;
}

vector<int>x;
int ans;
int n;
void dfs(int i,int cnt,int lcm)///i代表下标
{                              ///cnt代表几个数
    lcm=get_lcm(x[i],lcm);     ///lcm代表最小公倍数
    if(cnt&1)
        ans+=n/lcm;
    else
        ans-=n/lcm;
    for(int j=i+1;j<x.size();j++)
        dfs(j,cnt+1,lcm);
}
int main()
{
    int m;
    int temp;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        n--;
        x.clear();
        ans=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&temp);
            if(temp)
                x.push_back(temp);
        }
        for(int i=0;i<x.size();i++)
            dfs(i,1,1);
            cout<<ans<<endl;
    }
    return 0;
}


静态分析工具COBOT与Testbed对比 Testbed为规则检查工具的代表,市场占有量非常大,但随着时间的推移,大家从规则匹配转向了用静态分析工具检查Bug,所以规则检查型工具占有量比开始时候已经减少,但仍有一些比较落后的单位还在继续采购此类工具。3、通过对开源工程的测试,进行误漏报率的对比,发现COBOT(库博)的误漏报率相比Testbed较低,因COBOT主打国内市场,在众多军工单位进行大量试用及使用,吸取了众多单位的意见和建议,更加符合国内军工单位的使用习惯及测试要求。4、此外库博在功能上还可以进行计划任务,检测队列,检测并发的功能支持。 阅读详情

相关推荐

不止于算力:深入鲲鹏920的片上网络(NoC)与一致性协议HCCS,看华为如何优化数据流转

本文深入解析了鲲鹏920处理器的片上网络(NoC)架构和一致性协议HCCS,揭示了华为如何通过创新的环总线设计和私有协议优化数据流转效率。文章详细介绍了鲲鹏920在多die、多芯片环境下的高效协同机制,包括环总线架构、HCCS协议精妙设计及物理实现创新,为云服务场景提供了高性能解决方案。

weixin_30681121的博客 252

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

Ansys Lumerical实战:5步搞定Mach-Zehnder调制器多物理场仿真(附眼图分析技巧)

本文详细介绍了使用Ansys Lumerical进行行波马赫-曾德尔调制器多物理场仿真的完整流程。通过CHARGE、MODE和INTERCONNECT三大模块的协同工作,从电学参数提取、光学射频特性分析到系统级眼图评估,分步解析了高效仿真与性能优化的核心技巧,为硅光调制器设计提供了实战指南。

weixin_29056781的博客 542

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

Minecraft Forge模组开发入门:从环境搭建到事件监听实战

事件驱动编程是一种基于事件和事件监听器的软件架构模式,其核心原理是通过事件总线实现组件间的松耦合通信。在游戏开发领域,这种模式能够有效处理玩家交互、状态变更等异步操作,具有高度的灵活性和可扩展性。Java作为面向对象编程语言,通过注解和反射机制为事件驱动提供了天然支持。Minecraft模组开发正是事件驱动编程的典型应用场景,开发者可以利用Forge框架提供的事件总线监听游戏内各种行为。本文以Minecraft Forge模组开发为例,详细讲解如何通过Gradle构建工具搭建开发环境,并实现自定义方块注册和

culi3182的博客 395

电赛经验分享——赛前准备

⏩ 大家好哇!我是小光,想要成为系统架构师的嵌入式爱好者。⏩在之前的电赛中取得了省一的成绩,本文对电赛比赛前需要准备什么做一个经验分享。加入小光嵌入式交流群(qq群号:737327353)免费获取博主所有资料哦!

qq_52608074的博客 7779

HDU 1796How 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): 3315    Accepted Submission(s): 937 Problem Descriptio

布生气 1644

Codex 100个真实案例 - 用AI做英语单词计划学习工具(记忆曲线+测验)

背单词是英语学习的基本功,但市面上的背单词 APP 要么收费、要么广告太多。本案例用 Codex 从零打造一个 **英语单词计划学习工具**,实现 SM-2 间隔重复算法(Anki 的核心算法)、单词本增删改查、多种测验模式(选择题/拼写/听写)、TTS 发音播放,以及学习数据的可视化统计图表。全程用自然语言驱动 Codex,用 Python 写后端逻辑 + 终端 UI,打造你的私人背单词助手!

yueyeguzhuo的专栏 201

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

qq_41555192的博客 242

uni-app - 最详细 H5 网页接入微信登录功能,提供公众号配置与详细注释示例代码(移动端网页实现点击登录按钮后 调用微信公众号授权登录功能 详细讲解接入流程与详细示例代码)官方最新超级详细教程

uniapp中H5微信登录,uniapp H5微信授权登录,微信公众号第三方网页(H5)授权登录(OAuth2.0)流程及逻辑详细教程,UNIAPP 开发H5 怎么实现微信公众号登录功能,uniapp如何实现H5网页的公众号微信登录,怎么做uniapp微信登录,uniapp h5项目怎么实现微信登录的功能,uniapp微信浏览器H5授权微信登录,UniApp开发H5接入微信登录的全过程,uniapp的H5页面使用微信登录,uniapp H5微信授权登录,H5之微信授权登陆 (uniapp网页版微信授权登录)

🏆 前端领域优质创作者 7409

Python爬虫实战:汽车之家车型参数与口碑数据抓取分析

在当今信息爆炸的时代,购车决策已从传统的4S店咨询转变为数据驱动的智能化过程。汽车之家作为国内领先的汽车信息平台,汇聚了海量车型参数、车主口碑和专业评测数据。本文将详细介绍如何使用Python最新爬虫技术构建一个高效、稳定的汽车数据采集系统,通过多维度数据分析辅助用户做出更明智的购车决策。本项目将采用异步爬虫、智能反爬绕过、数据清洗与分析等现代化技术栈,完整实现从数据采集到可视化分析的全流程。本文详细介绍了如何使用Python最新技术构建一个功能完整的汽车之家数据爬虫系统。

2201_76125261的博客 1488

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

日月 362

YOLOv8 | 损失函数改进,最新提出的Shape-IoU,有效涨点

作为检测器定位分支的重要组成,边框回归损失在目标检测任务中发挥巨大作用。现有的边框回归方法,通常考虑了GT框与预测框之间的几何关系,通过使用边框间的相对位置与相对形状等计算损失,而忽略了边框其自身的形状与尺度等固有属性对边框回归的影响。为了弥补现有研究的不足,本文提出聚焦边框自身形状与尺度的边框回归方法。首先我们对边框回归特性进行分析,得出边框自身形状因素与尺度因素会对回归结果产生影响。

kay_545 1913

HDU 1796 How many integers can you find

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 i

平爷ZXP 858

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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中有多少数可以被该整数集合某个元素整除。 解题思路:需要用到容斥定理。根据集合里的元素,遇奇则加,遇偶则减(其实就是羡慕别人一对对...

。。。的博客 211

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

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

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

1565

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

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

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

hdu 1796 How many integers can you find

文章目录题目链接: 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1796 十年前的题了。。。。 我一看这个,很常规的二进制来容斥,心里想的是10分钟搞定,结果被坑了。。。 ①:这道题有0,第一发RE了。。。 ②:给的数不是互质的,哇让我长见识了,平时我们做的题都是互质的来容斥,然后我就想把公共的因子提出来,结果还是WA了,因为会有1的影响,比如1...

帅气廖鹏飞 281

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

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

weixin_43244265的博客 308

hdu1796 How many integers can you find(容斥)

题意: 给你一个数nnn,再给你mmm个数,问你小于nnn且至少是这mmm个数某个的倍数的数字有多少个。 思路 很明显的容斥,一开始用dfsdfsdfs写,已知Wa,后来看别人博客才知道有0输入,所以要去掉0…真坑。 dfsdfsdfs做法 ll f[N]; ll ans; ll n, m; ll LCM(ll a, ll b) { return a / __gcd(a, b) * b; } vo...

一身清贫怎敢入繁华 383

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): 8740    Accepted Submission(s): 2594 Pro

zhiyuanjiang的博客 312

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(容斥定理)

HDU - 1796 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 intege...

weixin_43179892的博客 234

HDU 1796 How many integers can you find 【容斥】

<题目链接> 题目大意: 给你m个数,其中可能含有0,问有多少小于n的正数能整除这个m个数中的某一个。 解题分析: 容斥水题,直接对这m个数(除0以外)及其组合的倍数在[1,n)中的个数即可,因为可能会重复计算,所以在叠加的时候进行容斥处理,下面用的是位运算实现容斥。 1 #include <cstdio> 2 #include <cstrin...

weixin_30364147的博客 115
上一篇: Counting Divisors HDU - 6069
下一篇: 度度熊与邪恶大魔王 百度之星
Grady_Ne
博客等级 码龄10年 45粉丝 177原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值