How many integers can you find

Problem Description
  Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.
 
Input
  There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.
 
Output
  For each case, output the number.
 
Sample Input
12 2 2 3
 
Sample Output
7
 

题意:

   给你m个数的集合,给你一个n问你小于n并且是这m个数里面的数的倍数有多少

题解:

   dfs容斥原理

   奇数偶数加减法

#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+, M = 1e6+, mod = 1e9+,inf = 1e9+; typedef long long ll; ll ans;
int x,a[N],n,m;
ll gcd(ll a,ll b) {return b==?a:gcd(b,a%b);}
void dfs(int i,int num,ll tmp) {
if(i>=m) {
if(num==) ans=;
else {
if(num&) ans = (ans+n/tmp);
else ans = ans - n/tmp;
}
return ;
}
dfs(i+,num,tmp);
dfs(i+,num+,tmp*a[i]/gcd(tmp,a[i]));
}
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
int k = ;
n--;
for(int i=;i<=m;i++) {
scanf("%d",&x);
if(x) a[k++] = x;
}
m = k;
ans = ;
dfs(,,);
printf("%I64d\n",ans);
}
return ;
}

 

HDU 1796 How many integers can you find 容斥入门的更多相关文章

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

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

  3. HDU 1796 How many integers can you find (容斥)

    题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n ...

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

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

  5. HDU 1796 How many integers can you find (状态压缩 + 容斥原理)

    题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 ...

  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(容斥原理+二进制/DFS)

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

  8. HDU How many integers can you find 容斥

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

  9. How many integers can you find(容斥+dfs容斥)

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

随机推荐

  1. Entity Framework Fluent API

    前言 使用DataAnnotation非常简单,但对于EntityFramework中的特性,就要在实体类中引入EntityFramework程序集,但实体类最好能是保持与架构无关性的POCO类,才能 ...

  2. BC.5200.Trees(dp)

    Trees  Accepts: 156  Submissions: 533  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/6 ...

  3. Spring常用的接口和类(三)

    一.CustomEditorConfigurer类 CustomEditorConfigurer可以读取实现java.beans.PropertyEditor接口的类,将字符串转为指定的类型.更方便的 ...

  4. poj1789 Truck History

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  5. OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能

    http://www.hopesoft.org/blog/?p=1188 http://www.imagemagick.org/download/ 2.用法 原始图片是input.jpg,尺寸:160 ...

  6. datetime中strftime和strptime用法

    from datetime import * format = "%Y-%m-%d %H:%M:%S" a=datetime.now() day=a.day b=a.replace ...

  7. JVM调优浅谈

    1.数据类型 java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:它代表的值就是数值本身,而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本 ...

  8. 《ASP.NET MVC4 WEB编程》学习笔记------Model模型绑定

    本文转载自haiziguo Asp.net mvc中的模型绑定,或许大家经常用,但是具体说他是怎么一回事,可能还是会有些陌生,那么,本文就带你理解模型绑定.为了理解模型绑定,本文会先给出其定义,然后对 ...

  9. java Long的iniValue出错

    Long l1 = 2500000000L; l1.intValue() 的值是负数 这里 System.out.println(Integer.MAX_VALUE); // 2147483647最大 ...

  10. windows下不打开浏览器访问网页的方法

    我们打开电脑,大多时候都是打开浏览器在上网.这都是通过浏览器来实现的,然而windows下有没有办法不通过浏览器也可以像linux那样达到访问网页的目的呢?这当然少不了批处理或者VBScript.然而 ...