How many integers can you find

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

Total Submission(s): 6048    Accepted Submission(s): 1735

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
 

题目链接:

pid=1796">点击打开链接

给出n, m, n代表1 - n的一个序列, 接下来m个数组成的集合, 问序列中能够整除任一集合中的一个数的个数和为多少.

对读入的m个数进行推断, 非0则赋值到a数组中, 进行dfs, dfs时进行容斥运算, id为奇数则加, 为偶数则减去反复的.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 15;
int n, m, num, ans, a[MAXN];
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
void dfs(int cur, int lcm, int id)
{
lcm = a[cur] / gcd(a[cur], lcm) * lcm;
if(id & 1) ans += (n - 1) / lcm;
else ans -= (n - 1) / lcm;
for(int i = cur + 1; i < num; ++i)
dfs(i, lcm, id + 1);
}
int main(int argc, char const *argv[])
{
while(scanf("%d%d", &n, &m) != EOF) {
num = ans = 0;
while(m--) {
int x;
scanf("%d", &x);
if(x != 0) a[num++] = x;
}
for(int i = 0; i < num; ++i)
dfs(i, a[i], 1);
printf("%d\n", ans);
}
return 0;
}

HDOJ1796 How many integers can you find(dfs+容斥)的更多相关文章

  1. How many integers can you find(容斥+dfs容斥)

    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. 15ecjtu校赛1006 (dfs容斥)

    Problem Description 在平面上有一个n*n的网格,即有n条平行于x轴的直线和n条平行于y轴的直线,形 成了n*n个交点(a,b)(1<=a<=n,1<=b<= ...

  4. 【BZOJ1853/2393】[Scoi2010]幸运数字/Cirno的完美算数教室 DFS+容斥

    [BZOJ1853][Scoi2010]幸运数字 Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那 ...

  5. 广州工业大学2016校赛 F 我是好人4 dfs+容斥

    Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...

  6. P2567 [SCOI2010]幸运数字 DFS+容斥定理

    P2567 [SCOI2010]幸运数字 题目描述 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,66 ...

  7. Eddy's爱好(dfs+容斥)

    Eddy's爱好 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. HDU 1796 How many integers can you find 【容斥】

    <题目链接> 题目大意: 给你m个数,其中可能含有0,问有多少小于n的正数能整除这个m个数中的某一个. 解题分析: 容斥水题,直接对这m个数(除0以外)及其组合的倍数在[1,n)中的个数即 ...

  9. HDU 1796 How many integers can you find(容斥)题解

    思路:二进制解决容斥问题,就和昨天做的差不多.但是这里题目给的因子不是质因子,所以我们求多个因子相乘时要算最小公倍数.题目所给的因数为非负数,故可能有0,如果因子为0就要删除. 代码: #includ ...

随机推荐

  1. POJ 1236 Tarjan算法

    这道题认真想了想.. [ 题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 2:至少需要添加几条 ...

  2. webapi时间字段返回格式设置及返回model首字母小写

    GlobalConfiguration.Configuration.Formatters.Remove(new XmlMediaTypeFormatter()); // 解决json序列化时的循环引用 ...

  3. DeltaFish 校园物资共享平台 第一次小组会议

    软工小组第一次会议 会议地点:图书馆 会议时间:19:00 ~ 20:00 与会人员:软工小组全体成员 请假人员:无缺席人员:无 记录人:陈志锴 整理人:曾子轩 会议记录 一.确认选题 每一位成员提出 ...

  4. js date 转化为字符串函数

    getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31). getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6). getFullYear() 从 Date 对象 ...

  5. VHDL之concurrent之when

    WHEN (simple and selected) It is one of the fundamental concurrent statements (along with operators ...

  6. Java_Web之神奇的Ajax

    为什么使用Ajax? 无刷新:不刷新整个页面,只刷新局部 无刷新的好处 提供类似C/S的交互效果,操作更方面 只更新部分页面,有效利用带宽   什么是Ajax?   XMLHttpRequest常用方 ...

  7. 揭开jQuery的面纱-jQuery选择器简介(二)

    选择器并没有一个固定的定义,在某种程度上说,jQuery的选择器和样式表中的选择器十分相似.选择器具有如下特点: 1.简化代码的编写 2.隐式迭代 3.无须判断对象是否存在 “$”是选择器不可缺少的部 ...

  8. https 认证总结

    https 握手阶段 服务器会下发一个证书链: 客户端默认的处理是使用系统的根证书对这个证书链进行合法性认证: 自签名证书,则需要自己完成证书链认证: 其他附属信息认证: 说白了就是一个数据检查的过程 ...

  9. C 预处理程序指令(CPP)

    #include 文件 提供的东西 stdio 提供 FILE.stdin.stdout.stderr 和 fprintf() 函数系列 stdlib 提供 malloc().calloc()和 re ...

  10. element ui table(表格)点击一行展开

    element ui是一个非常不错的vue的UI框架,element对table进行了封装,简化了vue对表格的渲染. element ui表格中有一个功能是展开行,在2.0版本官网例子中,只可以点击 ...