B. Which floor?

B. Which floor?
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;题意说每层楼的房间数是一样的,房间从第一层开始从1编号。给定编号i的房间所在的楼层,问能不能唯一确定编号为n的房间所在的楼层。
思路:题意每层最多100个房间,从最小遍历到100;在符合所有已给的编号的楼层后,求得的n所在的楼层数有大于一种结果的话,就不能唯一确定。
#include<bits/stdc++.h>
using namespace std;
int a[110];
int b[110];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int dw=1;//初始值不能是0,(输入1,0时)
    for(int i=1;i<=m;i++)
    {
      scanf("%d%d",&b[i],&a[i]);
      int aa=b[i]/a[i]+(b[i]%a[i]==0?0:1);
      dw=max(dw,aa);
    }
   int tmp1=n/dw+(n%dw==0?0:1);
   int ans=1;
   int j;
   for(int i=dw+1;i<=100;i++)
   {
       for(j=1;j<=m;j++)
       {
           int ss=b[j]/i+(b[j]%i==0?0:1);
           if(ss!=a[j]) break;
       }
       if(j>m)
       {
           int ff=n/i+(n%i==0?0:1);
           if(ff!=tmp1)
            ans++;
       }

       if(ans>=2) break;
   }
   if(ans==1) printf("%d\n",tmp1);
   else printf("-1\n");
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值