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 K (Java/Others)
Total Submission(s): 5556    Accepted Submission(s): 1593
all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.
12 2
2 3
7
题目大意:
求n以内可以被所给的集合中的数整除的数的个数。
解题思路:
这里要运用我们所说的容斥原理。
所谓容斥原理,运用起来要记住“奇加偶减”。
比方求100以内能被2,3,11,13,41整除的数的个数,我们即u(i)为100以内能被i整除的数的个数。
那么答案就是:
u(2)+u(3)+u(11)+u(13)+u(41)
-u(2*3)-u(3*11)-u(11*13)-u(13*41)
+u(2*3*11)+u(3*11*13)+u(11*13*41)
-u(2*3*11*13)-u(3*11*13*41)
+u(2*3*11*13*41)
这就是所谓的“奇加偶减”。
同一时候n以内能被i整除的数的个数为(n-1)/i。
综上。我们就能够通过枚举集合中的数,再容斥来得到答案。
枚举有2中方法:暴力枚举和dfs。因为m最大仅仅有10。暴力枚举时我们能够使用二进制来代表某个状态,每一位代表去与不取。dfs就非常easy了。
參考代码:
/*
二进制
Memory: 1568 KB Time: 639 MS
Language: G++ Result: Accepted
*/
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MAXN=25;
typedef long long LL; int n,m,num[MAXN],divi[MAXN]; int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
} int lcm(int a,int b)
{
return a/gcd(a,b)*b;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(scanf("%d%d",&n,&m)!=EOF)
{
int cnt=0;
for(int i=0;i<m;i++)
{
scanf("%d",&num[i]);
if(num[i])
divi[cnt++]=num[i];
}
m=cnt;
int ans=0;
for(int k=1;k<(1<<m);k++)
{
int select=0,tlcm=1;
for(int i=0;i<m;i++)
{
if(k&(1<<i))
{
select++;
tlcm=lcm(tlcm,divi[i]);
}
}
if(select&1)
ans+=(n-1)/tlcm;
else
ans-=(n-1)/tlcm;
}
printf("%d\n",ans);
}
return 0;
}
/*
dfs
Memory: 1572 KB Time: 109 MS
Language: G++ Result: Accepted
*/
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MAXN=25;
typedef long long LL; int n,m,num[MAXN],divi[MAXN],ans; int gcd(int a,int b)
{
return b? gcd(b,a%b):a;
} int lcm(int a,int b)
{
return a/gcd(a,b)*b;
} void dfs(int pos,int tlcm,int select)
{
//if(pos>m)
// return ;
tlcm=lcm(tlcm,divi[pos]);
select++;
if(select&1)
ans+=(n-1)/tlcm;
else
ans-=(n-1)/tlcm;
for(int i=pos+1;i<m;i++)
dfs(i,tlcm,select);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(scanf("%d%d",&n,&m)!=EOF)
{
int cnt=0;
for(int i=0; i<m; i++)
{
scanf("%d",&num[i]);
if(num[i])
divi[cnt++]=num[i];
}
m=cnt;
ans=0;
for(int i=0;i<m;i++)
dfs(i,1,0);
printf("%d\n",ans);
}
return 0;
}
HDU 1796 How many integers can you find(容斥原理+二进制/DFS)的更多相关文章
- 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(容斥原理)
		
题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举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  容斥入门
		
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
		
容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #i ...
 
随机推荐
- git的使用 及一些常见的错误处理
			
git安装使用 添加文件到Git仓库,分两步: 1.使用命令git add <file>,注意,可反复多次使用,添加多个文件: 2.使用命令git commit -m <messag ...
 - mysql 查看存储过程 并导出
			
查询数据库中的存储过程 select * from mysql.proc where db = dbName and `type` = 'PROCEDURE' show procedure statu ...
 - 浅谈JS之text/javascript和application/javascript
			
问题描述: JS在IE8以下浏览器运行异常 代码: <script>标签是这样子写的: <script type="application/javascript" ...
 - 04CSS文本字体及排版
			
CSS文本字体 字体——font-family font-family:字体1,字体2,字体3,……:应用font-family属性可以一次定义多个字体,而在浏览器读取字体时, 会按照定义的先后顺序来 ...
 - 原生 js 上传图片
			
js <!doctype html> <html> <head> <meta charset="utf-8"> <title& ...
 - 第3节 mapreduce高级:10、11、分组求取topN
			
只要修改OrderReducer.java的reduce方法,修改为: int i = 0;for(NullWritable nullWritable:values){ if(i>=2) bre ...
 - thinkphp5中vendor的使用?
			
1.创建处理数组的类ArrayList.php <?php /** * ArrayList实现类 * @author liu21st <liu21st@gmail.com> */ c ...
 - docker安装配置lnmp
			
一.安装配置docker 1.下载docker:yum install -y docker 2.设置docker远程镜像地址为国内路径:curl -sSL https://get.daocloud.i ...
 - Python之使用eval()函数将字符串的数据结构提取出来
			
data = input('请输入你要修改的对象:').strip() ''' 输入下面的字典列表 [{'backend':'www.oldboy1.org','record':{'server':' ...
 - layer弹层content写错导致div复制了一次,导致id失效 $().val() 获取不到dispaly:none div里表单的值
			
 错误之源: $("a.consult").click(function () { lib_consult_html = $('#consult-html').h ...