HDU 1796 How many integers can you find 【容斥】
<题目链接>
题目大意:
给你m个数,其中可能含有0,问有多少小于n的正数能整除这个m个数中的某一个。
解题分析:
容斥水题,直接对这m个数(除0以外)及其组合的倍数在[1,n)中的个数即可,因为可能会重复计算,所以在叠加的时候进行容斥处理,下面用的是位运算实现容斥。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
ll n,m,arr[];
ll gcd(ll a,ll b){
return b==?a:gcd(b,a%b);
}
ll lcm(ll a,ll b){
return a*b/gcd(a,b);
}
int main(){
while(cin>>n>>m){
int cnt=;
for(int i=;i<=m;i++){
scanf("%lld",&arr[cnt]);
if(arr[cnt])cnt++;
}
ll sum=;
for(int i=;i<(<<cnt);i++){
ll res=,tot=;
for(int j=;j<cnt;j++){
if(i & (<<j)){
res=lcm(res,arr[j]); //注意这里将不同的数组合时,是求它们的lcm,而不是直接相乘
tot++; //组合的数个数,用于后面判奇偶
}
}
if(tot & )sum+=(n-)/res; //题意不包含n
else sum-=(n-)/res;
}
printf("%lld\n",sum);
}
}
2019-02-09
HDU 1796 How many integers can you find 【容斥】的更多相关文章
- hdu 1796 How many integers can you find 容斥定理
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu 1796 How many integers can you find 容斥第一题
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 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, y ...
- HDU 1796 How many integers can you find (容斥)
题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n ...
- HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...
- HDU 1796 How many integers can you find (状态压缩 + 容斥原理)
题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 ...
- HDU 1796 How many integers can you find(容斥原理)
题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description ...
- HDU 1796 How many integers can you find(容斥原理+二进制/DFS)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU How many integers can you find 容斥
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- How many integers can you find(容斥+dfs容斥)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- 安装python的pip模块
安装python的pip模块 网址https://pypi.python.org/pypi/pip 选择,点击下载 将文件解压到C:\Users\Administrator\AppData\Local ...
- Confluence 6 查看内容索引概要
内容索引,通常也被称为查找索引,这个索引被用来在 Confluence 中支持查找.这个索引同时也被其他的一些功能使用,例如在归档邮件中构建邮件主题,View Space Activity 的特性和将 ...
- 关于ios进入后台界面后 播放声音解决方案
1 最近我在做环信视频通话时,遇到了一个新功能就是APP在后台的时候能对方能视频或者音频过来的时候 能够播放声音 根据查询相关资料得到如下解决办法 NSError *error; AVAudioSes ...
- ORM 对象关系映射
ORM (object relation mapping) 就是将对象数据转换为sql语句并执行 对象关系映射框架 orm 需要做的事情 1 生成创建表的语句 2 插入数据的语句 3 删除数据的语句 ...
- linux文件与目录管理笔记
### Linux文件与目录管理 ---------- 绝对路径: / 相对路径:不以/开头的 当前目录 . 上一个工作目录 - 用户主目录 ~ root账户的主目录是/root 其他用户是/home ...
- webpack2配置备份
package.json: { "name": "leyi", "version": "1.0.0", "ma ...
- LeetCode(77):组合
Medium! 题目描述: 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3] ...
- 【python】安装hyperscan-python
环境centos6.2 1. 先安装hyperscan 主要参考http://blog.csdn.net/cheng_fangang/article/details/51143412 注意:里面2.8 ...
- logical_backup: expdp/impdp
Table of Contents 1. 注意事项 2. 前期准备 3. 常用参数及示例 4. 常用语句示例 5. 交互式命令 6. 技巧 6.1. 不生成文件直接导入目标数据库 6.2. 通过she ...
- jquery实现全选 反选 取消
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...