How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7439    Accepted Submission(s): 2200

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
 
Author
wangye
 
Source
 
题意:给你m个数 可能含有0   问有多少小于n的正数能整除这个m个数中的某一个
题解:特判除零  容斥原理   这m个数 组合时不能直接相乘 应当取最小公倍数
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
ll gcd(ll aa,ll bb)
{
if(bb==)
return aa;
else
return gcd(bb,aa%bb);
}
ll lcm(ll aa,ll bb)
{
return aa*bb/gcd(aa,bb);
}
ll n,m;
ll que[];
ll a[];
ll ggg;
ll coun;
ll slove(ll gg)
{
ll t=,sum=;
a[t++]=-;
for(ll i=;i<coun;i++)
{ ll k=t;
for(ll j=;j<k;j++)
{
a[t++]=que[i]*a[j]*(-);
if(a[t-]>)
a[t-]=lcm(que[i],-a[j]);
else
a[t-]=-lcm(que[i],a[j]);
}
}
for(ll i=;i<t;i++)
sum=sum+(gg/a[i]);
return sum;
}
int main()
{
while(scanf("%I64d %I64d",&n,&m)!=EOF)
{
memset(que,,sizeof(que));
memset(a,,sizeof(a));
coun=;
for(ll i=;i<m;i++)
{
scanf("%I64d",&ggg);
if(ggg!=)
que[coun++]=ggg;
}
printf("%I64d\n",slove(n-));
}
return ;
}
 

HDU 1796 容斥原理的更多相关文章

  1. HDU 1796 容斥原理 How many integers can you find

    题目连接   http://acm.hdu.edu.cn/showproblem.php?pid=1796 处男容斥原理  纪念一下  TMD看了好久才明白DFS... 先贴代码后解释 #includ ...

  2. HDU 1796 (容斥原理)

    容斥原理练习题,忘记处理gcd 和 lcm,wa了几发0.0. #include<iostream> #include<cstdio> #include<cstring& ...

  3. hdu 1796(容斥原理+状态压缩)

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

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

    题意: 给你一个数n,找出来区间[1,n]内有多少书和n不互质 题解: 容斥原理 这一道题就让我真正了解容斥原理的实体部分 "容斥原理+枚举状态,碰到奇数加上(n-1)/lcm(a,b,c. ...

  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 ( 组合数学 容斥原理 二进制枚举)

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

  7. GCD HDU - 1695 容斥原理(复杂度低的版本)

    题意: 让你从区间[a,b]里面找一个数x,在区间[c,d]里面找一个数y.题目上已经设定a=b=1了.问你能找到多少对GCD(x,y)=k.x=5,y=7和y=5,x=7是同一对 题解: 弄了半天才 ...

  8. - Visible Trees HDU - 2841 容斥原理

    题意: 给你一个n*m的矩形,在1到m行,和1到n列上都有一棵树,问你站在(0,0)位置能看到多少棵树 题解: 用(x,y)表示某棵树的位置,那么只要x与y互质,那么这棵树就能被看到.不互质的话说明前 ...

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

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

随机推荐

  1. 448. Find All Numbers Disappeared in an Array Add to List

    题目描述 题目分析 有个[1,n]的条件要充分利用起来. 题目代码 public class Solution { public List<Integer> findDisappeared ...

  2. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. ajax是异步的,异步取数据,如何能保证数据是存在的。

    https://segmentfault.com/q/1010000002964172 ajax是异步的,所以aa()在执行的时候,return的那个result本身就是空的,在此后的某些秒以后,re ...

  4. JavaScript 获取当前时间戳

    转自博客(http://blog.sina.com.cn/s/blog_8772845101019kg5.html) JavaScript 获取当前时间戳:第一种方法: var timestamp = ...

  5. Android SDK Manager 更新不了文件 提示https://dl-ssl.google.com refused

    sdk manager无法自动更新,总在提示超时!!!SDK更新时的“https://dl-ssl.google.com refused”错误 解决方法: 在Android SDK Manager-& ...

  6. Shell重定向文件描述符

    #!/bin/bash      最近在看shell,各种困惑,不过解决困惑的感觉还是很不错的.废话少说,linux中使用文件描述符来标识每个文件对象.文件描述符为一个非负整数,可以唯一标识会话中打开 ...

  7. Hbase的WAL在RegionServer基本调用过程

    版权声明:本文由熊训德原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/221 来源:腾云阁 https://www.qclo ...

  8. Tomcat下使用war包发布项目

    Tomcat下使用war包发布项目 转自<Tomcat下使用war包发布项目 >,地址:http://blog.csdn.net/wy818/article/details/7240294 ...

  9. Android 系统ID介绍

    Android上系统ID有很多,本文只介绍常用的ANDROID ID.DEVICE ID.IMEI/MEID.WIFI/BT ADDRESS等几个,本文介绍这些ID的数据格式.长度及一些基本知识. 一 ...

  10. ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题

    想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContex ...