How many integers can you find

Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
分析:这几天练容斥有感觉,知道是容斥,但是却有问题,容斥是 互质的数,然后对于2,4这样的数就不会做了,太肤浅了,直接求最小公倍数啊,对啊,互质的相乘就是因为最小公倍数就是乘积啊=_=,弱!
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
int num[], n, m, a[];
LL res;
LL gcd(LL a, LL b)
{
if (a == )
return b;
return gcd(b % a, a);
}
void dfs(int cur, int snum, int cnt)
{
if (snum == cnt)
{
int temp = n;
int mult = ;
for (int i = ; i < snum; i++)
mult = mult / gcd(mult, a[i]) * a[i]; // 防爆
if (mult == )
return;
if (temp % mult == )
res += temp / mult - ;
else
res += temp / mult;
return;
}
for (int i = cur; i < m; i++)
{
a[snum] = num[i];
dfs(i + , snum + , cnt);
}
}
int main()
{
int tm;
while (scanf("%d%d", &n, &tm) != EOF)
{
m = ;
for (int i = ; i < tm; i++)
{
int temp;
scanf("%d", &temp); // 去0
if (temp)
num[m++] = temp;
}
LL sum = ;
for (int i = ; i <= m; i++)
{
res = ;
dfs(, , i);
if (i & )
sum += res;
else
sum -= res;
}
printf("%I64d\n", sum);
}
return ;
}

HDU 1796How many integers can you find(容斥原理)的更多相关文章

  1. HDU 1796 Howmany integers can you find (容斥原理)

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

  2. HDU 1796How 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(容斥原理)

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

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

    题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...

  5. HDU 1695 GCD (欧拉函数+容斥原理)

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

  6. HDU 5768:Lucky7(中国剩余定理 + 容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description   When ?? was born, seven ...

  7. HDU 4336 Card Collector 数学期望(容斥原理)

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意简单,直接用容斥原理即可 AC代码: #include <iostream> ...

  8. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  9. hdu1796 How many integers can you find 容斥原理

    Now you get a number N, and a M-integers set, you should find out how many integers which are small ...

随机推荐

  1. 海康威视,大华,宇视 的视频监控iOS Demo

    原谅我只提供一个链接,我在这里写了两遍,最后加个链接页面卡死了,下面的demo,最好真机调试.(写博客还是在别的地方写复制到这里比较好!) 一个画面可以做,4个,9个,16个画面原理是一样的,集合到自 ...

  2. Oracle行转列、列转行的Sql语句总结

    多行转字符串 这个比较简单,用||或concat函数可以实现  SQL Code  12    select concat(id,username) str from app_userselect i ...

  3. head/tail实现

         只实现了head/tail的基本功能,默认显示十行及-n参数.       一.使用带缓冲的系统调用.       write/read等系统调用是不带缓冲的,可以包装一层,使其带缓冲. t ...

  4. chrome防止自动填充密码

    是防止,不是禁止.禁止需要在浏览器设置. chrome浏览器保存密码之后,页面上有password存在的时候会出现自动填充用户名和密码的情况. 添加disableautocomplete和autoco ...

  5. ngx_http_fastcgi_module模块.md

    ngx_http_fastcgi_module ngx_http_fastcgi_module模块允许将请求传递到FastCGI服务器. fastcgi_bind Syntax: fastcgi_bi ...

  6. rxjs5.X系列 —— filter系列 api 笔记

    欢迎指导与讨论 :) 前言 本文是笔者翻译 RxJS 5.X 官网各类operation操作系列的的第二篇 -- filter转换.如有错漏,希望大家指出提醒O(∩_∩)O.更详细的资料尽在rxjs官 ...

  7. [LeetCode] Self Crossing 自交

    You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...

  8. .NET WebAPI 用ExceptionFilterAttribute实现错误(异常)日志的记录(log4net做写库操作)

    好吧,还是那个社区APP,非管理系统,用户行为日志感觉不是很必要的,但是,错误日志咱还是得记录则个.总不能上线后报bug了让自己手足无措吧,虽然不管有木有错误日志报bug都是件很头疼的事... 我们知 ...

  9. docker 常用命令(*)

    查找镜像 https://hub.docker.com/ search  --> centos7 一般docker 会有一个基础镜像,中间件镜像,应用镜像,生成一个镜像 docker build ...

  10. 求n!最后一位非零数

    引子:求n!末尾0的个数 n!末尾的0来源只有2,5两个质数相乘.所以只需要考察n!中包含多少个2和多少个5.然后取其较小值即为所求.即ans=min(cnt(2),cnt(5)).而转念一想,cnt ...