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

(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(容斥原理)的更多相关文章

  1. HDU 1796 Howmany integers can you find (容斥原理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. 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 ...

  3. 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 ...

  4. HDU 1796 How many integers can you find(容斥原理)

    题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...

  5. hdu分类 Math Theory(还有三题!)

    这个分类怎么觉得这么水呢.. 这个分类做到尾的模板集: //gcd int gcd(int a,int b){return b? gcd(b, a % b) : a;} //埃氏筛法 O(nlogn) ...

  6. HDU 1796 How many integers can you find(容斥原理)

    题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description    ...

  7. HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)

    HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...

  8. 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 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. xampp更改网站存放目录

    改完后重启xampp 如何更改监听端口8080

  2. 理解ASP.NET MVC中的ActionResult

    通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类 ...

  3. CRC-16校验C#代码

    [csharp] view plaincopyprint? using System; using System.Collections.Generic; using System.Text; usi ...

  4. redisTools-IdGenerator

    public class IdGenerator : RedisToolBase { //redis客户端对象 private static readonly NedisClient client = ...

  5. Effective C++ Item 17 Store newed objects in smart pointer in standalone statements

    If you trying to do multiple things in one statement, you should think carefully abnormal behavior e ...

  6. Spring装配Bean的过程补充

    对上一篇的<Spring装配Bean的过程>的过程说一下,不然真产生了误区. 误区在哪里呢?那就是spring bean的作用域问题. 说哈常用的两种作用域:默认是scope = sing ...

  7. [转载]会引起全表扫描的几种SQL

    查询语句的时候尽量避免全表扫描,使用全扫描,索引扫描!会引起全表扫描的几种SQL如下 1.模糊查询效率很低: 原因:like本身效率就比较低,应该尽量避免查询条件使用like:对于like ‘%... ...

  8. ios 监听设备旋转方向

    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { if(fromI ...

  9. PHP移除json数据最右侧的逗号!

    具体函数是:PHP rtrim() 函数 参考地址: http://www.w3school.com.cn/php/func_string_rtrim.asp 参考: <!DOCTYPE htm ...

  10. 【转载】web开发中 web 容器的作用(如tomcat)

    我们讲到servlet可以理解服务器端处理数据的java小程序,那么谁来负责管理servlet呢?这时候我们就要用到web容器.它帮助我们管理着servlet等,使我们只需要将重心专注于业务逻辑. 什 ...