题目链接:点击查看

题目大意:给出一个n,再给出一个含有m个数的集合,问1~n-1中有多少个数可以被集合中的所有数字整除

题目分析:因为1~n-1中的每个数可能会被整除多次,所以我们可以利用容斥原理枚举集合,奇加偶减,从而就能算出正确的答案了,类似于之前计算有多少种互斥的数那样写一个dfs就好,不过需要修改一个细节,就是累乘的地方要写成求lcm,因为一个数若想被集合中的所有数整除,只需要被其lcm整除就够了,再就是这个题目中会出现0,输入时预处理将0去掉就好了,最后就是记得开longlong,计算的过程中可能会爆int

代码:

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;LL n,ans;int m,tot,a[N];void dfs(int step,int cnt,LL sum)
{if(step==tot+1){if(cnt){if(cnt&1)ans+=n/sum;elseans-=n/sum;}return;}dfs(step+1,cnt,sum);dfs(step+1,cnt+1,sum*a[step]/__gcd(sum,(LL)a[step]));
}int main()
{
//  freopen("input.txt","r",stdin);
//  ios::sync_with_stdio(false);while(scanf("%lld%d",&n,&m)!=EOF){ans=0;n--;tot=0;for(int i=1;i<=m;i++){int val;scanf("%d",&val);if(val)a[++tot]=val;}dfs(1,0,1);printf("%lld\n",ans);}return 0;
}

HDU - 1796 How many integers can you find(容斥原理)相关推荐

  1. hdu 1796 How many integers can you find 容斥定理

    一开始看 这里 这个文章博主写得很好. 当举容斥定理的所谓 奇数为负 偶数为正的时候. 我直接就认为是 a*b 了.实际上是lcm(a,b). 由于博文中的因子都是互素的(素数之间).所以lcm(a, ...

  2. 【 HDU - 1796】How many integers can you find (容斥原理,二进制枚举或者dfs)

    题干: Now you get a number N, and a M-integers set, you should find out how many integers which are sm ...

  3. 容斥原理学习(Hdu 4135,Hdu 1796)

    题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU - 1796——容斥原理+二进制枚举

    [题目描述] Now you get a number N, and a M-integers set, you should find out how many integers which are ...

  5. hdu 50722014鞍山现场赛C题(容斥原理+同色三角形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5072: 题意:找出一个3元集合使集合中的两两互质,或两两不互质.这样的集合的个数. 分析:将每个数都幻 ...

  6. 编译vuejs html,VueJs(2)---VueJs开发环境的搭建和讲解index.html如何被渲染

    VueJs开发环境的搭建和讲解初始框架 有关如何搭建vue.js框架我这看了一篇文章,自己也根据它进行搭建环境. 接下来对初始的框架进行讲解,只讲index.html是如何被渲染出来的. 一.启动项目 ...

  7. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  8. 容斥原理(二进制实现)

    链接: hdu 1796 How many integers can you find 代码: #include <bits/stdc++.h> using namespace std; ...

  9. ACM模块解析之 数论

    数  论 一.简介 数论是ACM中的重点内容.历年竞赛题目,一般都有1~2道题目与数论有密切关系.数论涉及的概念和算法很多,用途也非常广泛.掌握与数论有关的方法,是参赛者需要具备的必要技能.数论的学习 ...

最新文章

  1. 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  2. Nutch URL过滤配置规则
  3. 【OI】WERTYU UVa 10082
  4. python 使用记录
  5. 数学建模学习笔记——模糊综合评价模型(评价类,发放问卷一般不用)
  6. 进程句柄表初始化,扩展,插入删除句柄源码分析
  7. shell_之_find(查找)
  8. android分屏模式_浅谈 Android 7.0 多窗口分屏模式的实现
  9. Oracle 增加修改删除字段与添加注释
  10. 报错:Avoid adding reactive properties to a Vue instance or its root $data at runtime - declare it upfr
  11. ASPNET--Basic Info
  12. JavaScript的单线程性质以及定时器的工作原理
  13. 返回路径平面上的间隙_PCB EMC问题:最常见的返回路径不连续
  14. oracle中master实例,oracle数据库加密--wallet 实例
  15. 电子书下载:深入解析Windows操作系统第6版 Windows Internals 6th Part1, Part2
  16. backtrack-回溯搜索算法总结
  17. 身份证号码前六位查询表
  18. 流媒体管理服务器显示不可用,部署国标流媒体服务器成功后无法播放视频问题步骤排查...
  19. 步骤一:支付宝-查看PID和APPID信息步骤
  20. d3-axis坐标轴

热门文章

  1. 导入不了css,CSS不导入。
  2. Nacos源码NacosServiceRegistryAutoConfiguration
  3. feign 能干什么:
  4. 服务器启动时的leader选举
  5. curator分布式锁的基本使用
  6. Redis的诞生历程
  7. ActiveMQ入门-ActiveMQ环境搭建
  8. response的运行过程
  9. 集合元素处理(Stream方式)
  10. mysql的length函数和char_length中文字符长度计算函数