73 lines
1.8 KiB
Rust
73 lines
1.8 KiB
Rust
use std::io::{self, Write, BufWriter, BufRead};
|
|
use std::collections::{HashMap, BTreeMap};
|
|
|
|
|
|
macro_rules! parse_line {
|
|
($buff: ident, $($t: ty), +) => ({
|
|
let mut line = String::new();
|
|
$buff.read_line(&mut line).unwrap();
|
|
let mut iter = line.split_whitespace();
|
|
($(iter.next().unwrap().parse::<$t>().unwrap()),+)
|
|
})
|
|
}
|
|
|
|
fn find_gcd(a : u128, b : u128) -> u128{
|
|
if a < b {
|
|
return find_gcd(b, a);
|
|
} else if b == 0{
|
|
return a;
|
|
} else {
|
|
return find_gcd(b, a % b);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main(){
|
|
let stdin = io::stdin();
|
|
let mut lock_in = stdin.lock();
|
|
|
|
let n = parse_line!(lock_in, u128);
|
|
let mut candidate_m : u128 = 0;
|
|
let mut lower_limit : u128 = 0;
|
|
let mut current = 0u128;
|
|
let upper_limit = 9_000_000_000_000_000_000u128;
|
|
let mut test = true;
|
|
for _ in 0..n {
|
|
let (change, result) = parse_line!(lock_in, i128, u128);
|
|
if change + (current as i128) > 0{
|
|
if result == (change + (current as i128)) as u128 {
|
|
current = result;
|
|
continue;
|
|
} else {
|
|
println!("-1");
|
|
test = false;
|
|
break;
|
|
}
|
|
}
|
|
let change = (- change) as u128;
|
|
candidate_m = find_gcd(change + result - current, candidate_m);
|
|
lower_limit = lower_limit.max(result);
|
|
|
|
if candidate_m <= lower_limit {
|
|
println!("-1");
|
|
test = false;
|
|
break;
|
|
}
|
|
|
|
current = result;
|
|
}
|
|
|
|
if test{
|
|
if candidate_m == 0 {
|
|
println!("1");
|
|
} else if candidate_m > lower_limit && candidate_m <= upper_limit{
|
|
println!("{}", candidate_m);
|
|
} else {
|
|
println!("-1");
|
|
}
|
|
}
|
|
|
|
}
|