【 HDU - 1796】How many integers can you find (容斥原理,二进制枚举或者dfs)

题干:

  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的集合,集合元素为非负整数。为1...n内能被集合里任意一个数整除的数字个数。n<=2^31,m<=10

输入n和m,接下来m个数。多组输入数据。

解题报告:

    就是个简单的容斥啦~直接暴力、。但是用二进制的话时间挺紧凑的吧感觉。。。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX];
ll LCM(ll a,ll b) {return (a*b)/__gcd(a,b);
}
int main()
{ll n,m,tmp,tot,ans;while(~scanf("%lld%lld",&n,&m)) {tot=ans=0;for(int i = 1; i<=m; i++) {scanf("%lld",&tmp);if(tmp != 0) a[++tot] = tmp; }for(int i = 1; i<=(1<<tot)-1; i++) {ll k = 0,lcm = 1;for(int j = 0; j<=tot-1; j++) {if(i & (1<<j)) {k++;lcm =LCM(lcm,a[j+1]);}}if(k & 1) ans += (n-1) / lcm;else ans -= (n-1) / lcm;  }printf("%lld\n",ans);}return 0 ;}

总结:

  注意判0啊,不然就会出现Runtime Error(INTEGER_DIVIDE_BY_ZERO)、、、其实想想也是嘛,因为gcd没事,但是lcm的时候有问题,比如lcm(0,100) return (0*100)/100

这题还可以用dfs写:(234ms)

//234ms
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
int n,m,cnt;
ll ans,a[30];
ll LCM(ll a,ll b) {return (a*b)/__gcd(a,b);
}
void dfs(int cur,ll lcm,int id) {lcm=LCM(a[cur],lcm);  //递归找两个数的最小公倍数(其中一个数还是集合里面上一个两个数的最小公倍数)所以lcm表示的真实意义是很多个数的最小公倍数if(id&1) ans+=(n-1)/lcm;     //因为这题并不包含n本身,所以用n-1else ans-=(n-1)/lcm;for(int i=cur+1; i<cnt; i++)dfs(i,lcm,id+1);  //id+1是表示如果上一次是奇数个数的倍数那么这次就是找的是偶数个数的倍数
}int main() {while(~scanf("%d%d",&n,&m)) {cnt=0;int x;while(m--) {scanf("%d",&x);if(x!=0)a[cnt++]=x;}ans=0;for(int i=0; i<cnt; i++)dfs(i,a[i],1);printf("%lld\n",ans);}return 0;
}

另一种dfs:(218ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
int n,m,cnt;
ll ans,a[30];
ll LCM(ll a,ll b) {return (a*b)/__gcd(a,b);
}void dfs(int cur,ll lcm,int id) {if(cur == cnt) {if(id == 0) return ;if(id&1) ans += (n-1)/lcm;else ans-=(n-1)/lcm;return ;}dfs(cur+1,lcm,id);dfs(cur+1,LCM(lcm,a[cur+1]),id+1);
}int main() {while(~scanf("%d%d",&n,&m)) {cnt=0;int x;while(m--) {scanf("%d",&x);if(x!=0)a[++cnt]=x;}ans=0;dfs(1,a[1],1);dfs(1,1,0);printf("%lld\n",ans);}return 0;
}

附:

一种新奇的dfs的思路:(31msAC)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441323.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 1027B 】Numbers on the Chessboard (没有营养的找规律题,无聊题)

题干&#xff1a; You are given a chessboard of size nnnn. It is filled with numbers from 11 to n2n2 in the following way: the first ⌈n22⌉⌈n22⌉ numbers from 11 to ⌈n22⌉⌈n22⌉ are written in the cells with even sum of coordinates from left to right f…

【CodeForces - 1060C】Maximum Subrectangle (思维,预处理前缀和,dp,枚举长度)

题干&#xff1a; You are given two arrays aa and bb of positive integers, with length nn and mmrespectively. Let cc be an nmnm matrix, where ci,jai⋅bjci,jai⋅bj. You need to find a subrectangle of the matrix cc such that the sum of its elements is at m…

【Codeforces 631C 】Report(单调栈,思维模拟)

题干&#xff1a; Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that d…

【CodeForces - 215A】Bicycle Chain (水题)

题干&#xff1a; Vasyas bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th sta…

ubuntu 在线安装php,ubuntu在线安装LNMP

一直以来个人安装lamp环境都是源码编译的&#xff0c;这个过程呢其实也要去经历的&#xff0c;但是毕竟占用时间久&#xff0c;有些时候在做一些测试环境的时候&#xff0c;可以在线安装比较快源码编译nginx可看往期&#xff1a;Nginx的安装对于lnmp的在线安装&#xff0c;如下…

【CodeForces - 215B 】Olympic Medal (数学,公式推导)

题干&#xff1a; The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0 < r2 < r1)made of metal with density p1 g/cm3. The second part is an i…

【CodeForces - 215C 】Crosses (思维,图形题)

题干&#xff1a; There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number i and co…

Scaffold php,GitHub - yiiplus/scaffold: scaffold是一个基于Yii2高级项目模版工程化实现的应用程序...

Yii 2 Scaffold Project Kit易加-脚手架(scaffold)是一个基于Yii2高级项目模版工程化实现的应用程序&#xff0c;它将更加高效、规范和工程化的满足项目开发的需求。DIRECTORY STRUCTUREcommonconfig/ contains shared configurationsmail/ contains view files for e-mailsmod…

oracle修改某个数据类型,Oracle 修改某个字段的数据类型三种方式

1.将该列设置为null,再修改其类型(这样会丢失数据)2.最简单的方法&#xff1a;假设你的表名为 tab_targetcreate table test as select * from tab_target whre 12;alter table test modify (col_name number(5));insert into test select * from tab_target;drop table tab_t…

【EOJ Monthly 2018.10 - B】 莫干山奇遇 (思维构造,数学,数组,贪心)(总结)

题干&#xff1a; Time limit per test: 2.0 seconds Memory limit: 512 megabytes 出题人当然是希望出的题目有关 oxx&#xff0c;于是想方设法给题目配上一些有关 oxx 的背景故事&#xff0c;使得它看起来不那么无趣。但有的时候却无法引入合适的小姐姐&#xff0c;使得 o…

有奶瓶的linux系统,用U盘启动BEINI(奶瓶)系统

用U盘启动&#xff1a;奶瓶(beini)这个系统&#xff0c;是一款基于Tiny Core Linux 搭建的无线网络安全测试系统&#xff0c;当然由于它是用来安全测试的系统&#xff0c;因此在安全方面自然有着强大的功能。而且&#xff0c;这个系统非常简便易学&#xff0c;因此现在已经逐渐…

【CodeForces - 227A】Where do I Turn? (计算几何,叉积判断直线拐向)

题干&#xff1a; Trouble came from the overseas lands: a three-headed dragon Gorynych arrived. The dragon settled at point C and began to terrorize the residents of the surrounding villages. A brave hero decided to put an end to the dragon. He moved from…

linux内核镜像sd卡,【原创】Linux QT镜像的制作--制作SD卡启动盘

最近买了个新的开发板&#xff0c;原生的是Android操作系统&#xff0c;需要自己少个启动盘&#xff0c;制作LinuxQT操作系统。新的开发板带这个制作的源文件&#xff0c;要先把这个文件拷贝到虚拟机Ubunbtu的共享目录下。打开share文件下显示文件如下&#xff1a;打开文件夹命…

【CodeForces - 227C】Flying Saucer Segments (思维)

题干&#xff1a; An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives dont even have antennas on their heads!). The flying saucer, on which the brave pioneers set off, consists of three sections. …

清楚linux缓存文件,Linux删除文件 清除缓存

相信很多测试 经常会经历开发叫你清除缓存这种事。那我们要怎么清呢&#xff1f;一、首先&#xff0c;确认你要清除的缓存在哪个目录下&#xff0c;然后切换到该目录下&#xff0c;比如 我现在知道我的的缓存目录是在newerp这个目录下&#xff0c;则如图二、然后 执行命令 rm -…

win10 linux安卓模拟器,genymotion安卓模拟器在Window10中使用的问题

最近一段时间&#xff0c;把系统升级到了win10&#xff0c;然后悲催的事情出现了&#xff0c;genymotion挂了&#xff0c;根本就不能启动&#xff0c;而且还是2.6版本的genymotion&#xff0c;下面我把遇到的问题总结一下&#xff1a;首先&#xff0c;在我的win10系统中&#x…

cross_compile = arm-linux-,cross compile grpc for arm

8种机械键盘轴体对比本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f;This post will tell you how to cross compile gPRC static lib for ARM.前段时间尝试交叉编译gRPC遇到了不少的麻烦&#xff0c;写篇post记录一下。gRPCPreparat…

linux键盘映射默认,Linux 中的键盘映射

前面提到&#xff0c;X Window 直接处理了键盘的输入输出端口&#xff0c;因此&#xff0c;在 Linux 虚拟控制台下和 X Window 下使用不同的键盘映射方法。在 Lin对于英语来说&#xff0c;键盘上的字母键直接和英语字母表中的字母对应&#xff0c;但是对于非英语的语种来说&…

C语言用字符串sex储存,2005年计算机等级考试二级C语言全真标准预测试卷(2)

一、选择题(1&#xff5e;40题每题1分&#xff0c;41&#xff5e;50题每题2分&#xff0c;共60分)1.微型计算机的运算器、控制器及内存储器组合在一起&#xff0c;称之为()(本题分值&#xff1a;1分)A.ALUB.CPUC.MPUD.主机【正确答案】D2.下列存储器中&#xff0c;存取速度最快…

c语言全局变量SQR,c语言a/=SQR(k+m);是什么意思?

满意答案NightmareJJ2013.03.16采纳率&#xff1a;47% 等级&#xff1a;12已帮助&#xff1a;11233人先看第一个main() // 主函数{printf("%ld\n", fun(3));} //输出fun(3)这个函数的返回值&#xff0c;3就是下一个//函数中的n 值。long fun (int n) //定义fun函数…