#include #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using pii = pair; using vi = vector; template ostream& operator<<(ostream& os, const vector& v) { os << "sz=" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template ostream& operator<<(ostream& os, const pair& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = 1e9 + 7; template constexpr T INF = numeric_limits::max() / 100; ll P, R; ll W, H; template T gcd(const T a, const T b) { return (b != 0) ? gcd(b, a % b) : a; } template T extgcd(const T a, const T b, T& x, T& y) // ax+by=gcd(a,b) { T d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } template T inverse(const T a, const T mod) { if (gcd(a, mod) != 1) { // cerr << "No inverse" << endl; return -1; } T x, y; extgcd(a, mod, x, y); return (mod + x % mod) % mod; } template T divide(const T a, const T b, const T mod) // return x (s.t. ax=b)(remark: x is value of modulo (mod/gcd(a, mod))) { const T g = gcd(a, mod); if ((g + b % g) % g != 0) { // cerr << "Cannot divide" << endl; return -1; } else { return (mod + (inverse(a / g, mod / g) * (b / g)) % mod) % mod; } } ll power(const ll a, const ll n) { if (n == 0) { return 1; } if (n % 2 == 0) { const ll p = power(a, n / 2); return (p * p) % P; } else { return (a * power(a, n - 1)) % P; } } unordered_map table; ll step; ll logR(const ll a) // return log_R(a) { if (a == 1) { return 0; } ll val = a; for (ll i = 0; i < W; i++) { if (table.find(val) != table.end()) { return i * H + table[val]; } else { val = (val * step) % P; } } } pair square_root(const ll a) { if (a == 0) { return make_pair(0, 0); } else { const ll x2 = logR(a); if (x2 % 2 == 1) { return make_pair(-1, -1); } else { return make_pair(power(R, x2 / 2), power(R, (x2 + (P - 1)) / 2)); } } } int main() { cin >> P >> R; ll Q; cin >> Q; ll mini = INF; for (ll i = 1; i * i <= P - 1; i++) { if ((P - 1) % i == 0) { if (mini > ((P - 1) / i) + i * Q) { mini = ((P - 1) / i) + i * Q; H = (P - 1) / i; W = i; } } } for (ll i = 0; i < H; i++) { table[power(R, i)] = i; } step = power(R, (P - 1) - H); for (ll q = 0; q < Q; q++) { ll A, B, C; cin >> A >> B >> C; const ll a = logR(A); if (B != 0 and C != 0) { const ll sq = (P + (B * B - 4 * A * C) % P) % P; const pair root = square_root(sq); if (root.first == -1) { cout << -1 << endl; continue; } ll Z[4]; for (int i = 0; i < 4; i++) { if (i % 2 == 0) { if (i / 2 == 0) { Z[i] = (P + (-B + root.first) % P) % P; } else { Z[i] = (P + (-B + root.second) % P) % P; } } else { if (i / 2 == 0) { Z[i] = (P + (-B - root.first) % P) % P; } else { Z[i] = (P + (-B - root.second) % P) % P; } } } ll z[4]; vector X(4); for (int i = 0; i < 4; i++) { if (Z[i] == 0) { X[i] = 0; } else { z[i] = ((P - 1) + (logR(Z[i]) - a) % (P - 1)) % (P - 1); X[i] = power(R, z[i]); X[i] = (X[i] % 2 == 0) ? X[i] / 2 : (X[i] + P) / 2; } } sort(X.begin(), X.end()); X.erase(unique(X.begin(), X.end()), X.end()); for (int i = 0; i < X.size(); i++) { if (i != 0) { cout << " "; } cout << X[i]; } cout << endl; } else if (B != 0) { cout << 0 << " "; const ll b = logR(B); ll x = (P - 1) / 2 - (a - b); x = ((P - 1) + x % (P - 1)) % (P - 1); cout << power(R, x) << endl; } else if (C != 0) { const ll c = logR(C); ll x2 = (P - 1) / 2 - (a - c); x2 = ((P - 1) + x2 % (P - 1)) % (P - 1); if (x2 % 2 == 1) { cout << -1 << endl; } else { const ll x = x2 / 2; const ll y = x2 / 2 + (P - 1) / 2; const ll u = power(R, x); const ll v = power(R, y); cout << min(u, v) << " " << max(u, v) << endl; } } else { cout << 0 << endl; } } return 0; }