Solution S o l u t i o n
刚开始看错题了。。。其实就是一个FFT裸题
如果
p∤a,p∤b
p
∤
a
,
p
∤
b
ab≡k(modp)
a
b
≡
k
(
mod
p
)
是与
logGa+logGb≡logGk(modφ(p))
log
G
a
+
log
G
b
≡
log
G
k
(
mod
φ
(
p
)
)
等价的吧。。
那找个原根就好啦。。模 p p 等于的特判一下
爆int调了好久quq
#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> Pairs;
const int N = 1 << 18;
const double PI = acos(-1);
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
template<typename T>
inline void read(T &x) {
static char c; x = 0; int sgn = 0;
for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
struct com {
double r, i;
com() { }
com(double _r, double _i): r(_r), i(_i) { }
inline com operator +(const com &b) {
return com(r + b.r, i + b.i);
}
inline com operator -(const com &b) {
return com(r - b.r, i - b.i);
}
inline com operator *(const com &b) {
return com(r * b.r - i * b.i, r * b.i + b.r * i);
}
inline com operator /(const double &b) {
return com(r / b, i / b);
}
inline com operator /(const int &b) {
return com(r / b, i / b);
}
};
com a[N];
int num, l, L;
int cnt[N], b[N], ib[N], snt[N];
ll ans[N];
int R[N];
com ww[N], iw[N];
int test, n, m, x, G, ans0;
vector<int> fac;
inline int pow(int a, int b, int MOD) {
int c = 1;
while (b) {
if (b & 1) c = (ll)c * a % MOD;
b >>= 1; a = (ll)a * a % MOD;
}
return c;
}
void pre(int n) {
num = n;
com g = com(cos(PI * 2 / num), sin(PI * 2 / num));
ww[0] = iw[0] = com(1, 0);
for (int i = 1; i < num; i++) ww[i] = ww[i - 1] * g;
for (int i = 1; i < num; i++) iw[i] = ww[num - i];
}
void fft(com *a, int n, int f) {
static com x, y;
com *w = (f == 1) ? ww : iw;
for (int i = 0; i < n; i++)
if (R[i] > i) swap(a[i], a[R[i]]);
for (int i = 1; i < n; i <<= 1)
for (int j = 0; j < n; j += (i << 1))
for (int k = 0; k < i; k++) {
x = a[j + k];
y = a[j + k + i] * w[num / (i << 1) * k];
a[j + k] = x + y;
a[j + k + i] = x - y;
}
}
int getG(int P) {
int phi = P - 1, m = sqrt(phi);
fac.clear();
for (int i = 2; i <= m; i++)
if (phi % i == 0) {
fac.push_back(i);
fac.push_back(phi / i);
}
for (int g = 2; ; g++) {
int ord = 1;
for (int d: fac)
if (pow(g, d, P) == 1) { ord = 0; break;}
if (ord) return g;
}
}
int main() {
freopen("1.in", "r", stdin);
freopen("1.out","w",stdout);
read(test); pre(1 << 17);
while (test--) {
read(n); read(m); ans0 = 0;
for (int i = 0; i < m; i++) b[i] = -1, snt[i] = cnt[i] = 0;
for (int i = 1; i <= n; i++) {
read(x); ++cnt[x % m];
if (x) ++snt[(ll)x * x % m];
ans0 += (x % m == 0);
}
for (l = 1, L = 0; l < m; l <<= 1) ++L; l <<= 1;
for (int i = 0; i < l; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << L);
G = getG(m);
for (int i = 1, c = 0; b[i] == -1; i = (ll)i * G % m) b[i] = c++;
for (int i = 0; i < l; i++) a[i] = com(0, 0);
for (int i = 1; i < m; i++) a[b[i]] = a[b[i]] + com(cnt[i], 0);
fft(a, l, 1);
for (int i = 0; i < l; i++) a[i] = a[i] * a[i];
fft(a, l, -1);
for (int i = 0; i < m; i++) ans[i] = 0;
for (int i = 1; i < m; i++)
ans[i] = ((a[b[i]].r + a[b[i] + m - 1].r) / l - snt[i]) / 2 + 0.5;
printf("%lld\n", (ll)n * (n - 1) / 2 - (ll)(n - ans0) * (n - ans0 - 1) / 2);
for (int i = 1; i < m; i++)
printf("%lld\n", ans[i]);
}
return 0;
}