#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::Read; #[allow(dead_code)] fn getline() -> String { let mut ret = String::new(); std::io::stdin().read_line(&mut ret).ok().unwrap(); ret } fn get_word() -> String { let mut stdin = std::io::stdin(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = String::from_utf8(buf).unwrap(); return ret; } } } #[allow(dead_code)] fn get() -> T { get_word().parse().ok().unwrap() } /** * p is prime */ fn modsqrt(mut a: i64, p: i64) -> Option { a %= p; if a == 0 { return Some(0); } if p == 2 { return Some(a); } if powmod(a, (p - 1) / 2, p) != 1 { return None; } let mut b = 1; while powmod(b, (p - 1) / 2, p) == 1 { b += 1; } let mut e = 0; let mut m = p - 1; while m % 2 == 0 { m /= 2; e += 1; } let mut x = powmod(a, (m - 1) / 2, p); let mut y = a * (x * x % p) % p; x = x * a % p; let mut z = powmod(b, m, p); while y != 1 { let mut j = 0; let mut t = y; while t != 1 { j += 1; t = t * t % p; } assert!(j < e); z = powmod(z, 1 << (e - j - 1), p); x = x * z % p; z = z * z % p; y = y * z % p; e = j; } Some(x) } fn powmod(x: i64, mut e: i64, m: i64) -> i64 { let mut sum = 1; let mut cur = x % m; while e > 0 { if e % 2 != 0 { sum = sum * cur % m; } cur = cur * cur % m; e /= 2; } sum } fn solve() { let p: i64 = get(); let r: i64 = get(); let q = get(); for _ in 0 .. q { let a: i64 = get(); let b: i64 = get(); let c: i64 = get(); let b = b * powmod(a, p - 2, p) % p; let c = c * powmod(a, p - 2, p) % p; let ss = modsqrt((b * b + (2 * p - 4) * c) % p, p); match ss { Some(ss) => { let mut hs = Vec::new(); hs.push(((2 * p - b - ss) % p) * powmod(2, p - 2, p) % p); hs.push(((p - b + ss) % p) * powmod(2, p - 2, p) % p); hs.sort(); if hs[0] == hs[1] { println!("{}", hs[0]); } else { println!("{} {}", hs[0], hs[1]); } }, None => { println!("-1"); }, } } } fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); }