codeforces 858b

来源:互联网 发布:淘宝网刷销量 编辑:程序博客网 时间:2024/05/23 19:18

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 ≤ 1000 ≤ 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 ≤ 1001 ≤ 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 36 22 17 3
output
4
input
8 43 16 25 22 1
output
-1

暴力

#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;struct node{int num;int fl;}x[110];int main(){int n,m;scanf("%d%d",&n,&m);int k,f;int a=-1;for(int i=0;i<m;i++)scanf("%d%d",&x[i].num,&x[i].fl);int ans=-1;for(int i=1;i<=110;i++){int j;for(j=0;j<m;j++){int temp=x[j].num/i;if(x[j].num%i)temp++;if(temp!=x[j].fl)break;}if(j==m){int res=n/i+(n%i>0?1:0);if(ans!=-1&&ans!=res){printf("-1\n");return 0;}ans=res;}}printf("%d\n",ans);}



原创粉丝点击