HDU 1796 How many integers can you find (状态压缩 + 容斥原理)
题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算。
思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 } + sum{ 整除三个的数 }………………所以是奇加偶减,而整除 k 个数的数可以表示成 lcm(A1,A2,…,Ak) 的倍数的形式。所以算出最小公倍数,
//HDU 1796
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std ; LL sh[] ; LL gcd(LL a,LL b)
{
return b == ? a : gcd(b,a%b) ;
}
int main()
{
LL a,b ;
while(~scanf("%I64d %I64d",&a,&b))
{
int c ,j = ;
for(int i = ; i < b ; i++)
{
scanf("%d",&c) ;
if(c > && c < a)
sh[j++] = c ;
}
b = j;
// sort(sh,sh+b);
LL ans = ;
for(int i = ; i < ( << b) ; i++)//(1 << b)-1种情况
{
LL num = ,lcm = ;
for(int j = ; j < b ; j++)
{
if(i & ( << j))
{
num ++ ;
lcm = lcm*sh[j]/gcd(lcm,sh[j]) ;
}
}
if(num & )
ans += (a-)/lcm ;
else
ans -= (a-)/lcm ;
}
printf("%I64d\n",ans) ;
}
return ;
}
HDU 1796 How many integers can you find (状态压缩 + 容斥原理)的更多相关文章
- HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...
- ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 4114 Disney's FastPass(最短路+状态压缩)
Disney's FastPass Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 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 容斥入门
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 容斥定理
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(容斥原理+二进制/DFS)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- wpa_supplicant安装
http://blog.chinaunix.net/uid-28572323-id-3482376.html http://blog.chinaunix.net/uid-26921272-id-341 ...
- Go defer延迟执行
defer用于延迟执行,可以类比于java或c++中的析构函数. 查看一段示例代码: func Contents(filename string) (string, error) { //打开文件 f ...
- php取整函数floor(),round(),intval(),ceil()
ceil -- 进一法取整说明float ceil ( float value )返回不小于 value 的下一个整数,value 如果有小数部分则进一位.ceil() 返回的类型仍然是 float, ...
- Linq To SQLite by CRUD
1, 希望使用linqtoSQLite 来对数据库实现CRUD, 开发环境 VS2013, 1.1 在网上找到了 LINQ to DB T4 Models, 配置参考网址链接: http://www. ...
- MATLAB GUI程序设计中使文本框接收多行输入的方法
对于文本框来说 Max属性于Min属性数值之差小于等于1时,仅接收单行输入 大于1时,接受多行输入 对于多行情况,set/get到的String应为cell 本系列文章允许转载,转载请保留全文! [说 ...
- net core 实战之 redis 负载均衡和"高可用"实现
net core 实战之 redis 负载均衡和"高可用"实现 1.概述 分布式系统缓存已经变得不可或缺,本文主要阐述如何实现redis主从复制集群的负载均衡,以及 redis的& ...
- Querying mergeinfo requires version 3 of the FSFS filesystem schema
环境: jdk 1.7; svn 3.0.4; TortoiseSVN 1.7.13 Subversion 1.7.10; IntelliJ IDEA 13.1.1;win7 64位系统 之前那个 ...
- 测试web数据库的分布式事务atomikos 的三种数据源 SimpleDataSourceBean,AtomikosDataSourceBean,AtomikosNonXADataSourceBean
这2天学习了atomikos事务控制框架,其中看到有3种数据源,分别是,SimpleDataSourceBean,AtomikosDataSourceBean,AtomikosNonXADataSou ...
- 如何将后台传来的json反序列化为前端具体对象
//jQuery方式 var obj = $.parseJSON(json); .... //eval var obj = eval("("+json+")" ...
- 我教女朋友学编程html系列(7)—Html无序列表、自定义列表、有序列表及常用例子
昨天写的那篇文章<我教女朋友学编程Html系列(6)—Html常用表单控件>,基本上有1000人左右看了,那边文章是我站在前人的肩膀上修改来的,添加了截图和说明,合并了例子,使之更容易被初 ...