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. oracle数据库的TNS配置

    TNS简要介绍与应用 Oracle中TNS的完整定义:transparence Network Substrate透明网络底层,监听服务是它重要的一部分,不是全部,不要把TNS当作只是监听器. TNS ...

  2. linux下route命令使用实战

    配置路由表: 第一步: sudo route add -net 172.30.6.0 netmask 255.255.255.0 eth0 执行后可以使用命令route -n查看,查看内容如下: 17 ...

  3. My favorite bit torrent client for Ubuntu

    Deluge Bit Torrent ClientDeluge is among my favorite bit torrent clients for Ubuntu. It is fast, sli ...

  4. WebClient上传音频文件

    //WebClient上传音频文件 public string UploadVoice(string fileNamePath) { Voice model=new Voice(); string s ...

  5. linux显示-bash-4.2# 问题

    今天,安装配置完mysql后,重新连接的shell的时候显示的不是root@localhost # 了,而是显示的-bash-4.2# 提示信息: Last login: Tue Apr 5 00:3 ...

  6. Android 连接webservice(利用谷歌提供的jar包)

    Android开发,需要连接webservice,之前就想用谷歌提供的jar包,下载地址:http://pan.baidu.com/s/1hqMTUHe 把它下载下来粘贴到libs文件夹下即可: 网上 ...

  7. Head First 设计模式--2 观察者模式 解耦

    观察者模式定义了对象之间一对多的依赖,这样依赖,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新. 假如有这么一个项目,监控每天的天气状况WeatherData(温度,湿度,气压).有布告板 ...

  8. django中上传图片的写法(转)

    view参数 @csrf_exemptdef before_upload_avatar(request):    before = True    return render_to_response( ...

  9. cocos2d-x 的两大基类

    cocos2d-x 有两个重要的基类,一个管理引用计数的 Ref,别一个则定义许多基本属性的 Node. 在 cocos2d-x 中的基本概念 说到 create 函数的时候提到 cocos2d-x ...

  10. NPOI 操作Excel

    关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...