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 ...
随机推荐
- 九度oj 1348 数组中的逆序对
原题链接:http://ac.jobdu.com/problem.php?pid=1348 归并排序求逆序对... #include<algorithm> #include<iost ...
- C# socket 实现消息中心向消息平台 转发消息 (修改)
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using ...
- java intellij 写控制台程序 窗口程序
建一个空项目,建一个main函数 用application,就可以运行了 /** * Created by robin on 15/10/11. */public class hello { pu ...
- [转]tftp在put上传的时候显示File not found的解决办法
[转]tftp在put上传的时候显示File not found的解决办法 http://blog.163.com/pengcz%40126/blog/static/35908607201182433 ...
- c/c++常用代码--udp多播
#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <winsock.h ...
- 2.2孙鑫C++
1.继承 动物有 吃 睡 呼吸的方法 当然 鱼也有 不用重复再定义 1)public 那里都可以访问 #include <iostream.h> class Animal //类 基 ...
- MATLAB连通域标记函数
L = bwlabel(BW,n)返回一个和BW大小相同的L矩阵,包含了标记了BW中每个连通区域的类别标签,这些标签的值为1.2.num(连通区域的个数).n的值为4或8,表示是按4连通寻找区域,还是 ...
- WIN7笔记本利用命令AP热点
第一步:以管理员身份运行命令提示符:开始”---搜索栏输入“cmd----右键以“管理员身份运行”自己随便命名,第二步:运行命令:netsh wlan set hostednetwork mode=a ...
- 【CentOs】搭建svn服务器
参考资料: svn攻略: http://blog.csdn.net/colinchan/article/details/1865154 错误解决:http://hi.baidu.com/anglem/ ...
- Java 查询URL对应IP地址
/** * @ClassName TestSocket1 * @Version 1.0 * @Date 2014-9-26 上午10:19:36 */ public class TestSocket1 ...