博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CF Round 434 B. Which floor?】
阅读量:5240 次
发布时间:2019-06-14

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

time limit per test 1 second

memory limit per test 256 megabytes

input standard input

output standard output

In a building where Polycarp lives there are equal number of flats on each floor. Unfortunately, Polycarp don't remember how many flats are on each floor, but he remembers that the flats are numbered from 1 from lower to upper floors. That is, the first several flats are on the first floor, the next several flats are on the second and so on. Polycarp don't remember the total number of flats in the building, so you can consider the building to be infinitely high (i.e. there are infinitely many floors). Note that the floors are numbered from 1.

Polycarp remembers on which floors several flats are located. It is guaranteed that this information is not self-contradictory. It means that there exists a building with equal number of flats on each floor so that the flats from Polycarp's memory have the floors Polycarp remembers.

Given this information, is it possible to restore the exact floor for flat n?

Input

The first line contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 100), where n is the number of the flat you need to restore floor for, and m is the number of flats in Polycarp's memory.

m lines follow, describing the Polycarp's memory: each of these lines contains a pair of integers ki, fi (1 ≤ ki ≤ 100, 1 ≤ fi ≤ 100), which means that the flat ki is on the fi-th floor. All values ki are distinct.

It is guaranteed that the given information is not self-contradictory.

Output

Print the number of the floor in which the n-th flat is located, if it is possible to determine it in a unique way. Print -1 if it is not possible to uniquely restore this floor.

Examples

Input

10 3 6 2 2 1 7 3

Output

4

Input

8 4 3 1 6 2 5 2 2 1

Output

-1

Note

In the first example the 6-th flat is on the 2-nd floor, while the 7-th flat is on the 3-rd, so, the 6-th flat is the last on its floor and there are 3 flats on each floor. Thus, the 10-th flat is on the 4-th floor.

In the second example there can be 3 or 4 flats on each floor, so we can't restore the floor for the 8-th flat.

 

【翻译】一栋无限高的楼,每一层的房间数相同。现在从一楼开始为房间标号,一层标完才标下一层。给出n条信息和一个目标房间号m,接下来输入n个二元组(k,f)表示有一个编号为k的房间在f层。询问m号房间所在楼层是否确定?确定则输出所在楼层,否则输出-1。

 

题解:

     ①设每层楼有x个房间,对于每条信息可以获得一个x的范围。

     ②最后取n个范围的交集,判断是否是唯一的(l是否等于r)

#include
#include
#include
#define L(i) ceil(1.0*k[i]/f[i])#define go(i,a,b) for(int i=a;i<=b;i++)#define R(i) floor(1.0*(k[i]-1)/(f[i]-1))#define pos(x) (n/x+(n%x!=0))int n,m,f[102],k[102],l=-1,r=1e9;int main(){ scanf("%d%d",&n,&m); if(n==1){puts("1");return 0;} go(i,1,m)scanf("%d%d",k+i,f+i), l=std::max(l,f[i]>1?(int)L(i):k[i]), r=std::min(r,f[i]>1?(int)R(i):(int)1e9); printf("%d\n",pos(l)==pos(r)?pos(l):-1);return 0;}//Paul_Guderian

 

转载于:https://www.cnblogs.com/Damitu/p/7699296.html

你可能感兴趣的文章
IO multiplexing 与 非阻塞网络编程
查看>>
hdu4105  Electric wave
查看>>
基于内容的图片检索CBIR(Content Based Image Retrieval)简介
查看>>
线程androidAndroid ConditionVariable的用法
查看>>
程序电脑VS2008 应用程序配置不正确,未能启动该应用程序。重新安装程序可以修复此问题。解决方法...
查看>>
设置类UIColor使用colorWithRed定义颜色
查看>>
文件语音识别Google语音识别学习札记 - Windows PC机上测试语音识别Strut2教程-java教程...
查看>>
μC/OS-III---I笔记13---中断管理
查看>>
:after,:before,content
查看>>
FTTB FTTC FTTH FTTO FSA
查看>>
OpenAI Gym
查看>>
stap-prep 需要安装那些内核符号
查看>>
网易杭研后台技术中心的博客 -MYSQL :OOM
查看>>
第二章 数据通信的基础知识 计算机网络笔记 学堂在线 2.1 数据传输系统 2.2 信号...
查看>>
如何解决click事件的重复触发问题
查看>>
2016寒假自学笔记
查看>>
VC++2012编程演练数据结构《21》二叉排序树
查看>>
ZOJ 3537 Cake(凸包+区间DP)
查看>>
Java中常见的集合类比较
查看>>
python - 内置函数
查看>>