二次剩余模板

x 2 ≡ n ( m o d p ) x ^ 2 \equiv n \pmod p x2n(modp)

/*
  Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
    ll f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-')    f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f * x;
}

struct com {
    int real, image;
    com(int a = 0, int b = 0) : real(a), image(b) {}
};

int I2, p;

com operator * (com a, com b) {
    return com((1ll * a.real * b.real  + 1ll * a.image * b.image % p * I2) % p, (1ll * a.real * b.image + 1ll * a.image * b.real) % p);
}

com power(com a, int k) {
    com ans = 1;
    while(k) {
        if(k & 1) ans = ans * a;
        a = a * a;
        k >>= 1;
    }
    return ans;
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int T = read();
    mt19937 e(233);
    while(T--) {
        int n = read();
        p = read();
        if(n == 0) {
            puts("0");
            continue;
        }
        if(power(n, (p - 1) >> 1).real == p - 1) {
            puts("Hola!");
            continue;
        }
        uniform_int_distribution<int> r(0, p - 1);
        int a = r(e);
        while(power((1ll * a * a % p - n + p) % p, (p - 1) >> 1).real == 1) a = r(e);
        I2 = (1ll * a * a % p - n + p) % p;
        int x = power(com(a, 1), (p + 1) >> 1).real, y = p - x;
        if(x > y) swap(x, y);
        printf("%d %d\n", x, y);
    }
	return 0;
}