HDU1796How many integers can you find(容斥原理)
在计数时,必须注意无一重复,无一遗漏。为了使重叠部分不被重复计算,人们研究出一种新的计数方法,这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无重复,这种计数的方法称为容斥原理。
(1)两个集合容斥关系

(2)三个集合容斥关系

公式:

这就是所谓的奇加偶减。
贴个模版题:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1796
题目解析:
这个题有bug,m可能为0。然后知道奇加偶减这个东西后,就可以深搜了,将所有组合情况全列出来,然后求lcm就好了。
求1~(n-1)中被集合m中元素中整除的个数,没学容斥原理之前做这题肯定是会超时的。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,top;
__int64 a[];
int gcd(int A,int B)
{
return B==?A:gcd(B,A%B);
}
//now为当前点,num为已经加入容斥的个数,lcm记录容斥的过程值(lcm),结果
void dfs(int now,int num,__int64 lcm,__int64 &sum)
{
lcm=a[now]/gcd(a[now],lcm)*lcm;
if(num&) sum+=(n-)/lcm;
else sum-=(n-)/lcm;
for(int i=now+; i<top; i++)
dfs(i,num+,lcm,sum);
}
int main()
{
int xx;
while(scanf("%d%d",&n,&m)!=EOF)
{
top=;
for(int i=; i<m; i++)
{
scanf("%d",&xx);
if(xx!=)
{
a[top++]=xx;
}
}
__int64 sum=;
for(int i=; i<top; i++)
{
dfs(i,,a[i],sum);
}
printf("%I64d\n",sum);
}
return ;
}
HDU1796How many integers can you find(容斥原理)的更多相关文章
- HDU 1796 Howmany integers can you find (容斥原理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu1796 How many integers can you find 容斥原理
Now you get a number N, and a M-integers set, you should find out how many integers which are small ...
- 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分类 Math Theory(还有三题!)
这个分类怎么觉得这么水呢.. 这个分类做到尾的模板集: //gcd int gcd(int a,int b){return b? gcd(b, a % b) : a;} //埃氏筛法 O(nlogn) ...
- 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 ( 组合数学 容斥原理 二进制枚举)
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...
- 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 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- [java] jstack 查看死锁问题
package com.xwolf.java.thread.ch2; /** * Created by Administrator on 2016/1/4 0004. */ public class ...
- JBPM4.4_核心概念与相关API
1. 核心概念与相关API(Service API) 1.1. 概念:Process definition, process instance , execution 1.1.1. Process ...
- Java精选笔记_JSP技术
JSP技术 JSP概述 什么是JSP 在JSP全名是Java Server Page,它是建立在Servlet规范之上的动态网页开发技术. 在JSP文件中,HTML代码与Java代码共同存在,其中,H ...
- Effective C++ Item 19 Treat class design as type design
Too high class topic for me now ................... ................... ................... fill the ...
- Linux printf 命令
printf 命令用来格式化输出,用法如下: [keysystem@localhost ~]$ printf "%s\n" 1 2 3 4 1 2 3 4 [keysystem@l ...
- Android 使用DatePicker以及TimePicker显示当前日期和时间
课程内容1.介绍DatePicker和TimePicker两种实现动态输入日期和事件的功能2.介绍DatePickerDialog和TimePickerDialog来年耕种实现动态输入日期和事件的对话 ...
- vue中npm install 报错之一
报错原因: 这是因为文件phantomjs-2.1.1-windows.zip过大,网络不好,容易下载失败 PhantomJS not found on PATH 解决方案一: 选择用cnpm ins ...
- 关于hql语句的一些问题
1.student is not mapped问题: 在执行显示数据库数据的时候出错 大概提示说: errors: s.entr_Id student is not mapped 碰到这种情况一般是: ...
- Centos7 安装zabbix3.0 服务端 详细
参考: https://www.cnblogs.com/37yan/p/6879218.html http://blog.csdn.net/hao134838/article/details/5712 ...
- CodeForces - 512B Fox And Jumping[map优化dp]
B. Fox And Jumping time limit per test 2 seconds memory limit per test 256 megabytes input standard ...