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 ...
随机推荐
- Windows 消息以及消息处理算法--线程和消息队列详解
Windows以消息驱动的方式,使得线程能够通过处理消息来响应外界. Windows 为每个需要接受消息和处理消息的线程建立消息队列(包括发送消息队列,登记消息队列,输入消息队列,响应消息队列),其中 ...
- navicat连接远程数据库报错'client does not support authentication protocol requested by server consider ...'解决方案
[1.cmd终端连接远程mysql数据库方法] mysql -uhello -pworld -h192.168.1.88 -P3306 -Dmysql_oa mysql -u用户名 -p密码 -h ...
- PyTricks-Differebt ways to test multiple flags at once in
x, y, z = 0, 1, 0 if x == 1 or y == 1 or z == 1: print('passed') if 1 in (x, y, z): print('passed') ...
- ionic serve 报【ionic-app-scripts' 不是内部或外部命令 】问题解
Error: Cannot find module 'E:\angular\ionic0515\node_modules\@ionic\app-scripts' at Function.Modu ...
- Scrapy - 小说爬虫
实例解析 - 小说爬虫 页面分析 共有三级页面 一级页面 大目录 二级页面 章节目录 三级界面 章节内容 爬取准备 一级界面 http://www.daomubiji.com/ 二级页面xpath 直 ...
- springboot之路径拦截器
方式一:不推荐,在代码中添加路径 1.写一个拦截器,继承HandlerInterceptor类 import org.springframework.stereotype.Component; imp ...
- nginx ssl
SSL 私钥/etc/pki/CA/ (umask 077;openssl genrsa -out private/cakey.pem 2048) 自签证书 openssl req -new -x50 ...
- clientdataset的使用
clientdataset的使用(一) Delphi做为一个快速应用开发工具,深受程序员的喜爱.其强大的组件功能,让程序员能够轻松. 高效地完成常见的界面开发.数据库应用等功能.然而,帮助的相对缺 ...
- python 内置数据结构 字符串
字符串 一个个字符组成的有序的序列,是字符的集合 使用单引号,双引号,三引号引住的字符序列 字符串是不可变对象 Python3起,字符串就是Unicode类型 字符串定义 初始化 s1 = 'stri ...
- 【python画图】
1.热力图 import numpy as np import numpy.random import matplotlib.pyplot as plt # Generate some test da ...