博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ1796 How many integers can you find(dfs+容斥)
阅读量:6874 次
发布时间:2019-06-26

本文共 1876 字,大约阅读时间需要 6 分钟。

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
 

题目链接:

给出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;}

转载地址:http://kmofl.baihongyu.com/

你可能感兴趣的文章
JQuery Ajax 的简单使用
查看>>
Codeforces Round #287 (Div. 2) ABCDE
查看>>
【转载】读懂IL代码就这么简单(二)
查看>>
09-JS的事件流的概念(重点)
查看>>
有关inline-block
查看>>
文献随笔(九)
查看>>
git相关
查看>>
加入大型的js文件如jQuery文件,Eclipse会报错
查看>>
POJ 2763 (树链剖分+边修改+边查询)
查看>>
全局变量---只创建一次
查看>>
IOS APP上下黑边问题
查看>>
数位dp题集
查看>>
C# 汉字转拼音
查看>>
jquery实现复制的两种方式
查看>>
Django分页(一)
查看>>
Balance Adjustment页面调整无法保存的问题
查看>>
De Moivre–Laplace theorem
查看>>
symfony2使用form指定的checkbox,设置其属性disabled
查看>>
linux操作之软件安装(一)
查看>>
react 使用 lazyload 懒加载图片
查看>>