HDU1796 How many integers can you find【容斥定理】
题目链接:
http://acm.hdu.edu.cn/showproblem.php?
pid=1796
题目大意:
给你一个整数N。和M个整数的集合{A1、A2、…、Am}。集合内元素为非负数(包括零),求小于N的
正整数(1~N-1)中,能被M个整数的集合中随意一个元素整除的正整数个数。
比如N = 12。M = {2,3},在1~N-1中,能被2整除的数为{2,4,6。8。10},能被3整除的数为
{3。6,9}。则所求集合为{2,3,4。6,8,9,10},共7个,则答案为7。
思路:
就是求M个集合的并集。先看上边的样例。能被2整除的数集合S1为{2,4,6,8。10},能被3整除的数
集合S2为{3,6。9}。而两者交集S12为能被LCM(2,3) = 6整除的数为{6}。
则两者并集 S = S1 + S2 - S12。
依据容斥定理得:若有M个数,则可求出1~N-1中能被不同组合的公倍数整除的个数。
1~N-1能被公倍数整除的个数为 (N-1) / LCM。然后依据奇数项加,偶数项减的原则得出答案个数。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std; int Gcd(int a,int b)
{
if(a < b)
int temp = a, a = b, b = temp;
if(b == 0)
return a;
return Gcd(b,a%b);
} int Lcm(int a,int b)
{
return a/Gcd(a,b)*b;
} int N,M;
int A[220],Select[220]; int Solve()
{
int ans = 0;
for(int i = 1; i < (1 << M); ++i)
{
int odd = 0;
for(int j = 0; j < M; ++j)
{
if((1<<j) & i)
{
Select[++odd] = j;
}
}
int LCM = 1;
for(int j = 1; j <= odd; ++j)
LCM = Lcm(LCM,A[Select[j]]);
if(odd & 1)
ans += N/LCM;
else
ans -= N/LCM;
}
return ans;
} int main()
{
int d;
while(~scanf("%d%d",&N,&M))
{
for(int i = 0; i < M; ++i)
{
scanf("%d",&d);
if(d)
A[i] = d;
else
i--,M--;
}
N--;
printf("%d\n",Solve());
} return 0;
}
HDU1796 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 1796How many integers can you find(简单容斥定理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...
- How Many Sets I(容斥定理)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 4135 Co-prime 欧拉+容斥定理
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 1695 GCD(容斥定理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- TOJ 4008 The Leaf Eaters(容斥定理)
Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eat ...
- HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...
随机推荐
- Classes and functions
As another example of a user-defined type, we’ll define a class called Time that records the time of ...
- 如何启动和关闭MySQL?(四)
分为: 图形化 命令行 方法一:在“计算机”的图标上右键,然后选择“管理”,双击“服务和应用程序”,然后选择“服务”,找到“MySQL”这个服务,然后点击“右键”,就可以进行“停止”和“重新启动”的操 ...
- js base64加密与C#后台base64解密
js代码 function encode64(input) { var output = ""; var base = new Base64(); var output = bas ...
- YYDispatchQueuePool阅读笔记
阅读了开源大神的YYDispatchQueuePool,在此记下一些从中学到的东西. 首先YYDispatchQueuePool.m文件中有如下类型对应: static inline dispatch ...
- webService接口发布失败问题
今天在原有工程上新增加了个webService接口的服务类,但是总提示 axis2 出错 File "/axis2-web/listSingleService.jsp" not f ...
- GoldenGate 双向复制解决方案
1 双向复制方案简介 在双向复制(Bidirectional)方案中,可以采用以下两种部署方式: 方式一:配置源和目标数据库可以同时保持Active 状态,同时进行应用系统的事务处理, 此时需由应用系 ...
- tomcat动态查看服务器打印日志
[root@localhost ~]# cd /usr/local/tomcat/logs [root@localhost logs]# tail -f catalina.out FROM:htt ...
- Nginx的日志管理
vim /usr/local/nginx/conf/nginx.conf #编辑 nginx 配置文件 server{ ...
- ssh 免交互式登陆
脚本: vim key.sh #!/bin/bash#make key\rm -f /root/.ssh/id_dsassh-keygen -t dsa -f /root/.ssh/id_dsa -P ...
- 【http code】HTTP协议状态码详解(HTTP Status Code)
参考资料: http://www.cnblogs.com/shanyou/archive/2012/05/06/2486134.html 1xx:请求收到,这些状态代码表示临时的响应. 2xx:操作成 ...