#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; 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); ll ti; ll B; ll P, R; ll n, alpha; std::vector> expos; ll table[252521]; 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); int main(int argc, char** argv){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); ti = 7; // ti = std::stoi(argv[1]); 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{ ll m = babyStepGiantStep(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 = inverse(2ll * a % P, P); 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; } } } void init(){ B = sqrt(P) * ti; if(B == 0){B = P;} n = P / B; alpha = expt(R, B, P); for(ll i=0;i(lhs) < std::get<0>(rhs);}); if(it != expos.end() && std::get<0>(*it) == v){ ll c = B * i + std::get<1>(*it); 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); }