QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#139837 | #2447. Domino Covering | Qingyu | AC ✓ | 559ms | 3764kb | C++23 | 3.8kb | 2023-08-14 18:16:23 | 2023-08-14 18:16:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 35;
typedef long long LL;
typedef int Matrix[maxn][maxn];
int t, n, mod, cur, pre = 1, f[3][maxn << 1 | 1], deg[3];
LL m;
Matrix mat[3];
inline void mod_inc(int &x, int y) {
(x += y) >= mod && (x -= mod);
}
inline int mod_inv(int x) {
int y = mod, u = 1, v = 0;
while(x) {
int q = y / x;
swap(x, y);
x -= q * y;
swap(u, v);
u -= q * v;
}
assert(y == 1);
return v < 0 ? v + mod : v;
}
inline int mat_half_det(Matrix &mat) {
int odd = 1, even = 1;
for(int i = 0; i < n; ++i) {
int &det = i & 1 ? odd : even;
for(int j = i; j < n; ++j) {
if(!mat[j][i])
continue;
if(i == j)
break;
if(det)
det = mod - det;
for(int k = i; k < n; ++k)
swap(mat[i][k], mat[j][k]);
}
if(!mat[i][i]) {
det = 0;
continue;
}
det = (LL)det * mat[i][i] % mod;
for(int j = i, k = mod_inv(mat[i][i]); j < n; ++j)
mat[i][j] = (LL)mat[i][j] * k % mod;
for(int j = i + 1; j < n; ++j) {
if(!mat[j][i])
continue;
int d = mod - mat[j][i];
for(int k = i; k < n; ++k)
mat[j][k] = (mat[j][k] + (LL)d * mat[i][k]) % mod;
}
}
assert(odd == even);
return even;
}
inline void poly_mod() {
int *G = f[2];
for(int i = deg[cur] - n; i >= 0; --i) {
int *F = f[cur] + i;
if(!F[n])
continue;
int d = mod - F[n];
for(int j = 0; j <= n; ++j)
F[j] = (F[j] + (LL)d * G[j]) % mod;
}
for(deg[cur] > n && (deg[cur] = n); deg[cur] && !f[cur][deg[cur]]; --deg[cur]);
}
inline void mat_next() {
swap(cur, pre);
Matrix &F = mat[cur], &G = mat[pre];
for(int i = 0; i < n; ++i)
for(int j = 1; j < n; ++j) {
mod_inc(F[i][j], G[i][j - 1]);
mod_inc(F[i][j - 1], G[i][j]);
}
}
int main() {
scanf("%d", &t);
while(t--) {
scanf("%d%lld%d", &n, &m, &mod);
assert(mod > 1);
if((n & 1) && (m & 1)) {
puts("0");
continue;
}
if(n > m) {
int t = m;
m = n;
n = t;
}
if(n == 1) {
puts("1");
continue;
}
f[cur][0] = 1;
for(int i = 1; i <= n; ++i) {
swap(cur, pre);
f[cur][i] = f[pre][i - 1];
for(int j = i - 1; j > 0; --j) {
f[cur][j] = f[pre][j - 1];
mod_inc(f[cur][j], mod - f[pre][j]);
mod_inc(f[cur][j], mod - f[cur][j - 1]);
}
assert(f[pre][0]);
f[cur][0] = mod - f[pre][0];
}
memcpy(f[2], f[cur], (n + 1) * sizeof(int));
assert(f[2][n] == 1);
deg[cur] = 0;
f[cur][0] = 1;
int mx = 0;
for( ; (1LL << mx) <= (m >> 1); ++mx);
for(int i = mx - 1; i >= 0; --i) {
swap(cur, pre);
deg[cur] = deg[pre] << 1;
memset(f[cur], 0, (deg[cur] + 1) * sizeof(int));
for(int j = 0; j <= deg[pre]; ++j)
for(int k = 0; k <= deg[pre]; ++k)
f[cur][j + k] = (f[cur][j + k] + (LL)f[pre][j] * f[pre][k]) % mod;
poly_mod();
if((m >> (i + 1)) & 1) {
++deg[cur];
for(int i = deg[cur]; i > 0; --i)
f[cur][i] = f[cur][i - 1];
f[cur][0] = 0;
poly_mod();
}
}
deg[2] = deg[cur];
memcpy(f[2], f[cur], (deg[2] + 1) * sizeof(int));
for(int i = 0; i < n; ++i) {
memset(mat[0][i], 0, n * sizeof(int));
memset(mat[1][i], 0, n * sizeof(int));
memset(mat[2][i], 0, n * sizeof(int));
}
for(int i = 0; i <= deg[2]; ++i) {
if(i) {
mat_next();
mat_next();
} else {
for(int j = 0; j < n; ++j)
mat[cur][j][j] = 1;
if(m & 1)
mat_next();
}
if(f[2][i]) {
for(int j = 0; j < n; ++j)
for(int k = 0; k < n; ++k)
if(mat[cur][j][k])
mat[2][j][k] = (mat[2][j][k] + (LL)f[2][i] * mat[cur][j][k]) % mod;
}
}
if(m & 1)
for(int i = 0; i < n; i += 2)
for(int j = 0; j < n; ++j)
swap(mat[2][i][j], mat[2][i + 1][j]);
int ans = mat_half_det(mat[2]);
printf("%d\n", ans);
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 559ms
memory: 3764kb
input:
20000 3 695860418 1064851577 5 909984642 1024590071 2 702478034 1015656679 3 832070346 1020170803 3 931276816 1069777147 5 624464668 1019025517 4 563777828 1039054439 3 70355912 1062629389 2 538334151 1043751551 4 644616259 1051984399 2 565963832 1050482821 3 489913670 1030290631 5 625001688 6518147...
output:
175636008 670951730 483405543 127343329 519459233 524024279 815773299 734679810 266201944 156563661 556277524 841321357 607188444 243626454 108744246 7651374 636855097 509810366 66494941 482058350 232780855 220305284 157636415 735388230 99671148 863972210 540368496 824555091 20145756 337198629 85541...
result:
ok 20000 lines