B - 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.
题目大意:给你一个数n,和m个0~20的整数i,让你求1~n-1中是i的倍数的数有几个。
显然,这是一个容斥定理的题,求对1~(n-1)中所有m个i的倍数的数的个数。并且m<=10,因此我们用二进制枚举法就能够列出所有的情况。
需要注意的就是m个数,把这m个数先化成互质的。最后在套用模板即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[];
int main()
{
ll n, m;
while(~scanf("%lld%lld",&n,&m))
{ int top = ;
for(int i = ; i < m; ++ i)
{
ll x;
cin >> x;
if(x != )
a[top++] = x;
}
ll ans = ,sum,num;
for(int i = ; i < ( << top); ++ i)
{
num=,sum=;
for(int j = ; j < top; ++ j)
{
if((i >> j) & )
{
num++;
sum = sum * a[j] /(__gcd(sum, a[j]));
}
}
if(num % )
{
ans += (n - ) / sum;
}
else
{
ans -= (n - ) / sum;
}
}
cout << ans << endl;
}
return ;
}
以上。
B - How many integers can you find的更多相关文章
- [LeetCode] Sum of Two Integers 两数之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- LeetCode 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- 解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译)
解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译) http://improve.dk/the-anatomy-of-row-amp-page-compre ...
随机推荐
- Echarts案例-折线图
一:先在官网下载 https://www.echartsjs.com/zh/download.html 然后再建立工程,导入这两个包: 写代码: <!DOCTYPE html> <h ...
- [题解] [SDOI2017] 序列计数
题面 题解 和 SDOI2015 序列统计 比较像 这个无非就是把乘改成了加, NTT 改成了 MTT 再加上了一个小小的容斥 : 拿所有方案减去不合法方案即可 Code #include <a ...
- Ubuntu下Nginx的安装和卸载
环境是Ubuntu 16.04 安装: sudo apt-get update sudo apt-get install nginx 卸载: sudo apt-get --purge remove n ...
- 互联网IT当线上出现 bug 时,是怎么处理的?
线上BUG说处理方法:1.关于线上BUG问题,目前公司有一整套线上故障流程规范,包括故障定义.定级.处理流程.故障处理超时升级机制.故障处理小组.故障处罚(与故障存在时长有关)等:2.最主要的是,线上 ...
- 设置Python打印格式
>>> def esc(code): ... return f'\033[{code}m' ... >>> print(esc('31;1;4') + 'reall ...
- <JavaScript>谈谈javascript语法里一些难点问题(一)
1) 引子 前不久我建立的技术群里一位MM问了一个这样的问题,她贴出的代码如下所示: var a = 1; function hehe() { window.alert(a); var a = ...
- Linux通过AIO进行异步读文件
下面列出源代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <a ...
- Firefox Chrome Http请求插件
Firefox:HttpRequester Chrome:Advanced Rest Client
- List的remove方法里的坑
今天遇到一件怪事,用一个ArrayList添加了一个对象,再调用ArrayList的remove方法删除该对象,当然这时对象是数据库里查出来的,但内容绝对是一样,却发现remove失败了.演示一下,这 ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_16-异常处理-可预知异常处理-自定义异常类型和抛出类
在common工程创建捕获异常的类:CustomException Runtime叫做运行异常.在代码中抛出的话 对我们的代码没有可侵入性 如果在代码上抛出 如果改成Exception 这时候就会有错 ...