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. Android中webView和网页的交互

     Android中webView和网页的交互 Android中webView跟网页的交互式通过JavaScript进行的.具体步骤: 1.创建JavaScript,在点击的时候调用JavaScript ...

  2. 改变Activity启动时的默认动画

    why 在开发中我们可能需要改变这一行为,一般基于2种理由: 产品的spec这么要求的: 想自己控制这个行为,因为在Android设备上可能每个的这一默认行为都不一样,厂商可以很容易修改它,从而导致a ...

  3. mac 抓包工具charles v3.9.3 安装破解步骤

    一.下载 先到它的官网http://www.charlesproxy.com/可下载到最新版本,这个下载有点慢,我已经将它放到网盘中了:http://pan.baidu.com/s/1skTXRIl ...

  4. ORA-02020 : 过多的数据库链接在使用中-Windows环境解决步骤

    一.现象 编译存储过程时报ORA-02020错误. 错误详细信息:ORA-04052在查找远程对象 xx@yy时出错 ORA-00604 : 递归 SQL 级别 1 出现错误 ORA-02020 : ...

  5. mysql半同步复制问题排查

    1.问题背景      默认情况下,线上的mysql复制都是异步复制,因此在极端情况下,主备切换时,会有一定的概率备库比主库数据少,因此切换后,我们会通过工具进行回滚回补,确保数据不丢失.半同步复制则 ...

  6. 【MSSQL】MSSQL还原单mdf文件报1813错误

    序: MS SQL Server 2008 r2附加无ldf日志的mdf数据库时报1813错误.提示数据库被强制分离,无日志不能还原之类的话. 可能的原因: 原因系统正在执行定时作业,没有主要到多次强 ...

  7. java中使用mongodb的几种方式

    最近有时间看了一下mongodb,因为mongodb更容易扩展所以考虑使用mongodb来保存数据. 首先下载安装mongodb,这是很简单的,装好后使用mongod命令就可以启动数据库.正式部署的话 ...

  8. iOS开发常用代码块

    遍历可变数组的同时删除数组元素 NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array]; NSString *str1 = ...

  9. plain framework 1 网络流 缓存数据详解

    网络流是什么?为什么网络流中需要存在缓存数据?为什么PF中要采用缓存网络数据的机制?带着这几个疑问,让我们好好详细的了解一下在网络数据交互中我们容易忽视以及薄弱的一块.该部分为PF现有的网络流模型,但 ...

  10. [KMP]【学习笔记】

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Descript ...