How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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
思路:最简单的容斥,注意下可能输入0;奇加偶减
   比如12 2 
              2   3
     ans=11/2+11/3-11/6;
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
ll a[];
ll ji;
ll ans,x,y;
ll gcd(ll x,ll y)
{
return y==?x:gcd(y,x%y);
}
void dfs(ll lcm,ll pos,ll step)
{
if(lcm>x)
return;
if(pos==ji)
{
if(step==)
return;
if(step&)
ans+=x/lcm;
else
ans-=x/lcm;
return;
}
dfs(lcm,pos+,step);
dfs(lcm/gcd(a[pos],lcm)*a[pos],pos+,step+);
}
int main()
{
ll z,i,t;
while(~scanf("%I64d%I64d",&x,&y))
{
x--;
ji=;
for(i=;i<y;i++)
{
scanf("%I64d",&z);
if(z==)continue;
a[ji++]=z;
}
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 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.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)

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

  5. HDU 1796How many integers can you find(简单容斥定理)

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

  6. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. HDU1796 How many integers can you find【容斥定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包 ...

  8. hdu 4135 [a,b]中n互质数个数+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...

  9. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. [py模块]random&string取随机字符串

    栗子 - 取n位的随机字符串(大小写/数字) def get_random_str(len_str): import string import random letters_nums = strin ...

  2. POJ3096:Surprising Strings(map)

    http://poj.org/problem?id=3096 for循环真是奇妙! #include <string.h> #include <stdio.h> #includ ...

  3. 安插,复制,替换和删除ul中的li

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. spark shuffle原理

    1.spark中窄依赖的时候不需要shuffle,只有宽依赖的时候需要shuffle,mapreduce中map到reduce必须经过shuffle 2.spark中的shuffle fetch的时候 ...

  5. FormatMessage函数

    DWORD WINAPI FormatMessage( __in DWORD dwFlags, __in_opt LPCVOID lpSource, __in DWORD dwMessageId, _ ...

  6. SQL Server报“GUID应包含带4个短划线的32位数”

    转自:http://www.seayee.net/article/info_106.html 最近在配置一台服务器的MS SQL Server 2005的维护计划自动备份数据库,能创建维护计划,但设置 ...

  7. Keepalived保证Nginx高可用配置

    Keepalived保证Nginx高可用配置部署环境 keepalived-1.2.18 nginx-1.6.2 VM虚拟机redhat6.5-x64:192.168.1.201.192.168.1. ...

  8. python退出多重循环

    假设一段python程序有多重循环,我们都知道在一个循环当中,用break是退出当前的循环,然后继续下一次循环,但是如何才能跳出多重循环呢,实际就是结束所有的循环. 思路1::可以定义一个异常类,在需 ...

  9. 你真的了解微服务架构吗?听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构

    微服务架构是互联网很热门的话题,是互联网技术发展的必然结果.它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.虽然微服务架构没有公认的技术标准和规范或者草案,但业 ...

  10. Nginx 灰度实现方式(支持纯灰度,纯生产,50度灰及更多比例配置)

    前言 Nginx相关技术短信本篇幅不做详细介绍,所以学习本文之前要对Nginx有相关的了解. 生产环境即线上环境,在经历开发.测试再到上线,不可避免的会更新生产环境,但谁又能保证测试过的代码到线上运行 ...