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<math.h>
#include<stdio.h>
#include<algorithm>
#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

【CF Round 434 B. Which floor?】的更多相关文章

  1. 【CF Round 434 A. k-rounding】

    Time limit per test1 second memory limit per test 256 megabytes input standard input output standard ...

  2. 【Codeforces Round #434 (Div. 2) B】Which floor?

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举每层有多少个公寓就好. 要注意,每次都要从1到100判断,一下那个公寓该不该出现在那一层. 多个答案,如果答案是一样的.也算是唯一的.  ...

  3. 【Codeforces Round #434 (Div. 1) B】Polycarp's phone book

    [链接]h在这里写链接 [题意] 给你n个电话号码. 让你给每一个电话号码选定一个字符串s; 使得这个串s是这个电话号码的子串. 且不是任何一个其他电话号码的子串. s要求最短. [题解] 字典树. ...

  4. 【Codeforces Round #434 (Div. 2) A】k-rounding

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 转换一下就是求n和10^k的最小公倍数. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++ ...

  5. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  6. 【codeforces】【比赛题解】#861 CF Round #434 (Div.2)

    本来是rated,现在变成unrated,你说气不气. 链接. [A]k-凑整 题意: 一个正整数\(n\)的\(k\)-凑整数是最小的正整数\(x\)使得\(x\)在十进制下末尾有\(k\)个或更多 ...

  7. 【CF Round 429 B. Godsend】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 【CF Round 439 E. The Untended Antiquity】

    time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...

  9. 【CF Round 439 C. The Intriguing Obsession】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

随机推荐

  1. php+sqlserver处理读取decimal 类型数据,不满1的数字会去掉0的问题

    php+sqlserver处理读取decimal 类型数据,如果数据不满1,会去掉0的问题.比如读取的数据是 0.05,会显示 .05 function convert_number($number) ...

  2. php 无限参数方法

    在很多项目开发中经常会用到共用方法但是参数不固定,每个参数都创建一遍阅读性不好,后期维护也麻烦,PHP有获取传入参数的方法,记录参考一下.这里有两个方法 <?php 方法一: #不指定参数个数方 ...

  3. 数据库中pymysql模块的使用

    pymysql 模块 使用步骤: 核心类Connect链接用和Cursor读写用 1. 与数据库服务器建立链接 2. 获取游标对象(用于发送和接收数据) 3. 用游标执行sql语句 4. 使用fetc ...

  4. 【Markdown】Markdown的使用(自用)

    # 欢迎使用 Cmd Markdown 编辑阅读器 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们为记录 ...

  5. 关于VSCode如何缩进两个空格

    使用VSCode编写vue的时候,由于缩进问题经常报错.(默认缩进4个空格,实际规范上是两个空格) 更改VSCode的缩进格式. 但是此时你在编写代码的时候却发现任然缩进4格,此时因为vscode默认 ...

  6. (数据科学学习手札04)Python与R在自定义函数上的异同

    自编函数是几乎每一种编程语言的基础功能,有些时候我们需要解决的问题可能没有完全一致的包中的函数来进行解决,这个时候自编函数就成了一样利器,而Python与R在这方面也有着一定的差别,下面举例说明: P ...

  7. 反向代理服务器——nginx

    一.概述 先来看百度百科的介绍: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.其特点是占有内存少,并发能力强 ...

  8. 10 TCP 传输控制协议 UDP区别

    1.tcp和udp区别 2.TCP通信模型 生活中的电话机 如果想让别人能更够打通咱们的电话获取相应服务的话,需要做一下几件事情: 买个手机 插上手机卡 设计手机为正常接听状态(即能够响铃) 静静的等 ...

  9. I两种冒泡算法

    两种冒泡算法: 第一个循环,I 定位当前坐标,第二个循环 把 I 之后的每个数都与 I 比较(比 I 小的都去坐标I),第二个循环之后 坐标 I 为数组里最小的数值. 效率比较高的冒泡算法: stat ...

  10. Java - 问题集 - 导出csv文件中文乱码

    微软的excel文件需要通过文件头的bom来识别编码,所以写文件时,需要先写入bom头. FileOutputStream fos = new FileOutputStream(new File(&q ...