#include using namespace std; #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) using ll = long long; const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; void init(); ll babyStepGiantStep(ll b); template T expt(T a, T n, T mod = std::numeric_limits::max()); template T inverse(T n, T mod); std::tuple extgcd(ll a, ll b); std::tuple expos[100000]; ll P, R; map inv_map, bsgs_map; int main(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> P >> R; init(); ll g, s; tie(g, s, ignore) = extgcd(2ll, P-1); int Q; std::cin >> Q; for(int i=0;i> a >> b >> c; ll D = (b * b - 4ll * a * c) % P; D = D >= 0 ? D : D + P; ll sq; if(D == 0){ sq = 0; }else{ if(bsgs_map.find(D) == bsgs_map.end()){ bsgs_map[D] = babyStepGiantStep(D); } ll m = bsgs_map[D]; if(m % g != 0){ std::cout << -1ll << std::endl; continue; } ll t = s * (m / g) % (P - 1); t = t >= 0 ? t : t + (P - 1); sq = expt(R, t, P); } ll den = 2ll * a % P; if(inv_map.find(den) == inv_map.end()){ inv_map[den] = inverse(den, P); } den = inv_map[den]; ll x0 = (-b - sq) * den % P, x1 = (-b + sq) * den % P; x0 = x0 >= 0 ? x0 : x0 + P; x1 = x1 >= 0 ? x1 : x1 + P; if(x0 > x1){swap(x0, x1);} if(x0 == x1){ std::cout << x0 << std::endl; }else{ std::cout << x0 << " " << x1 << std::endl; } } } ll n, alpha; void init(){ n = std::floor(std::sqrt(P)); alpha = expt(R, n, P); for(int i=0;i(*it) == v){ ll c = n * i + std::get<1>(*it); if(c < P){ return c; } } } return -1ll; } std::tuple extgcd(ll a, ll b){ if(b == 0){ return std::make_tuple(a, 1ll, 0ll); } auto s = extgcd(b, a % b); return std::make_tuple(fst(s), thd(s), snd(s)-(a/b)*thd(s)); } template T expt(T a, T n, T mod){ T res = 1; while(n){ if(n & 1){res = res * a % mod;} a = a * a % mod; n >>= 1; } return res; } template inline T inverse(T n, T mod){ return expt(n, mod-2, mod); }