How many integers can you find

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

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

题目大意:给定n和一个大小为m的集合,集合元素为非负整数。为1...n内能被集合里任意一个数整除的数字个数。n<=2^31,m<=10

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-;
const int inf =0x7f7f7f7f;
const double pi=acos(-);
const int maxn=; ll gcd(ll a,ll b)
{
if(b==) return a;
else return gcd(b,a%b);
} ll lcm(ll a,ll b)
{
return (a/gcd(a,b))*b;
} int n,m,bit,mm[],tmp,cnt;
ll mult;
void solve(int flag)
{
mult=;bit=;
for(int i=;i<cnt;i++)
if(flag&(<<i))
{mult=lcm(mm[i],mult);bit++;}
} int main()
{
while(~scanf("%d %d",&n,&m))
{
ll ans=;n--;cnt=;
for(int i=;i<m;i++)
{
scanf("%d",&tmp);
if(tmp) mm[cnt++]=tmp;
}
for(int i=;i<(<<cnt);i++)
{
solve(i);
int num=((ll)n)/mult;
if(bit%==) ans+=num;
else ans-=num;
}
printf("%lld\n",ans);
}
return ;
}

分析:很好的一道容斥题,分析:求出在给定区间中能被集合中任意一个数整除的点的个数,分析题目的话
可以发现,先求出区间中所有能被集合中单个数整除的点的个数,求和后,会发现,能同时被两个数整除的点(是这两个数的最小公倍数的倍数)多算了一次,所以就减去能同时被两个数整除点的总个数,然后再加上能同时被三个点减去的点的个数.....(容斥),不过这个题目有个很大的坑点,就是必须要去0,否则不仅会导致re,而且还会直接导致错误,因为在下面这段代码中,如果cnt换成m的话。可以发现0的存在就直接导致了ans的值得变化,所以必须要在读入集合时就直接将0剔除

for(int i=1;i<(1<<cnt);i++)
{
solve(i);
int num=((ll)n)/mult;
if(bit%2==1) ans+=num;
else ans-=num;
}

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 Problem Description   Now you get a number N, and a M-integers set, y ...

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

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

  4. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

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

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

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

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

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

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

  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 How many integers can you find 容斥

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

随机推荐

  1. 2种方法实现java对象的深拷贝

    2种方法实现java对象的深拷贝 2017年12月03日 22:23:07 iCoding91 阅读数 4420更多 分类专栏: java   版权声明:本文为博主原创文章,遵循CC 4.0 BY-S ...

  2. 一般处理程序,ajax

    一般处理程序调用session: 在.aspx.cs页中读写Session都是Session["***"]就可以获取或者写入.但是在一般处理程序也就是ashx页面中,再这样写的话, ...

  3. Linq Distinct 自定义比较

    private class MyMenuComparer : IEqualityComparer { public bool Equals(ParMenu x, ParMenu y){ return ...

  4. 【Activiti】crm与工作流的整合,一个完整的流程实例创建到任务完成的过程

    1.建立任务列表页面--根据用户的nickName作为assignee查询其所拥有的任务列表 2.在任务后添加办理按钮 3.点击办理按钮,出现流程办理框,其中详细显示了该任务的相关详细信息,本实例中为 ...

  5. 本人亲测-inno setup打包EXE(较完整实例)

    ; Script generated by the Inno Setup Script Wizard.; SEE THE DOCUMENTATION FOR DETAILS ON CREATING I ...

  6. Java操作FTP,从FTP上读取指定文件,把指定文件上传到FTP

    需要添加的依赖 <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> ...

  7. Windows Electron初探

    最近闲来无事,玩玩electron. 1.安装nodejs 下载地址:http://nodejs.cn/download/,下载64位.安装完成后,打开C:\Program Files\nodejs\ ...

  8. 记录一次维护weblogic集群的问题

    [问题描述] weblogic 集群子服务节点启动,启动完毕后状态改为 ADMIN,正常情况是 RUNNING 在启动脚本添加如下配置即可 -DDomainRegistrationEnabled=tr ...

  9. 24、Nginx缓存web服务

    通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后端压力,提高网站并发延时 1.缓存常见类型 服务端缓存 代理缓存, 获取服务端内容进行缓存 客户端浏览器缓存 Nginx代理缓存原理 ...

  10. concurrent.futures:线程池,让你更加高效、并发的处理任务

    并发任务池 concurrent.futures模块提供了使用工作线程或进程池运行任务的接口. 线程池和进程池的API是一致的,所以应用只需要做最小的修改就可以在线程和进程之间进行切换 这个模块提供了 ...