QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#800943 | #8821. Nightmare | NotNotToday# | RE | 269ms | 6228kb | C++20 | 4.3kb | 2024-12-06 17:09:46 | 2024-12-06 17:09:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
const int P = 1e6 + 7;
int p;
int add(int a, int b) {
return a + b < p ? a + b : a + b - p;
}
int sub(int a, int b) {
return a - b >= 0 ? a - b : a - b + p;
}
int mul(int a, int b) {
return a * 1ll * b % p;
}
int pw(int a, int n) {
int ret = 1;
for (; n > 0; n >>= 1, a = mul(a, a)) {
if (n & 1) {
ret = mul(ret, a);
}
}
return ret;
}
struct elem_op {
int type, i, j, coef;
};
int n;
int a[N][N];
int v[N][N];
vector<elem_op> ops;
int root[P];
int m;
void row_add(int i, int j, int coef) {
for (int k = 0; k < n; ++k) {
a[i][k] = add(a[i][k], mul(a[j][k], coef));
}
}
void row_swap(int i, int j) {
for (int k = 0; k < n; ++k) {
swap(a[i][k], a[j][k]);
}
}
void col_add(int i, int j, int coef) {
for (int k = 0; k < n; ++k) {
a[k][i] = add(a[k][i], mul(a[k][j], coef));
}
}
void col_swap(int i, int j) {
for (int k = 0; k < n; ++k) {
swap(a[k][i], a[k][j]);
}
}
void multi_add(int i, int j, int coef) {
row_add(i, j, coef);
col_add(i, j, coef);
}
void multi_swap(int i, int j) {
row_swap(i, j);
col_swap(i, j);
}
void v_row_add(int i, int j, int coef) {
for (int k = 0; k < m; ++k) {
v[i][k] = add(v[i][k], mul(v[j][k], coef));
}
}
void v_row_swap(int i, int j) {
for (int k = 0; k < m; ++k) {
swap(v[i][k], v[j][k]);
}
}
void v_col_add(int i, int j, int coef) {
for (int k = 0; k < m; ++k) {
v[k][i] = add(v[k][i], mul(v[k][j], coef));
}
}
void v_col_swap(int i, int j) {
for (int k = 0; k < m; ++k) {
swap(v[k][i], v[k][j]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> p;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> a[i][j];
}
}
for (int k = 0; k < n; ++k) {
if (a[k][k] == 0) {
bool ok = 1;
for (int i = k + 1; i < n; ++i) {
ok &= a[k][i] == 0;
}
if (ok) {
continue;
}
if (p == 2) {
for (int i = k + 1; i < n; ++i) {
if (a[i][i] != 0) {
ok = 1;
multi_swap(k, i);
ops.push_back({1, k, i, -1});
break;
}
}
assert(ok);
} else {
for (int i = k + 1; i < n; ++i) {
if (a[k][i] != 0) {
multi_add(k, i, 1);
ops.push_back({0, k, i, 1});
if (a[k][k] == 0) {
while (true) {}
}
break;
}
}
}
}
int r = pw(a[k][k], p - 2);
for (int i = k + 1; i < n; ++i) {
if (a[k][i] != 0) {
int coef = sub(0, mul(a[k][i], r));
multi_add(i, k, coef);
ops.push_back({0, i, k, coef});
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (a[i][i] == 0 && a[j][j] != 0) {
multi_swap(i, j);
ops.push_back({1, i, j, -1});
}
}
}
for (int i = 0; i < n; ++i) {
if (a[i][i] != 0) {
++m;
}
}
for (int i = 0; i < p; ++i) {
root[mul(i, i)] = i;
}
for (int i = 0; i < m; ++i) {
v[i][i] = root[a[i][i]];
}
reverse(ops.begin(), ops.end());
for (auto op : ops) {
if (op.type == 0) {
v_row_add(op.i, op.j, sub(0, op.coef));
} else {
v_row_swap(op.i, op.j);
}
}
cout << m << "\n";
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << v[i][j] << " ";
}
cout << "\n";
}
// #ifdef LOCAL
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) {
// int sum = 0;
// for (int k = 0; k < m; ++k) {
// sum = add(sum, mul(v[i][k], v[j][k]));
// }
// cout << sum << " ";
// }
// cout << "\n";
// }
// #endif
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3684kb
input:
2 2 1 1 1 1
output:
1 1 1
result:
ok accepted
Test #2:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
3 5 4 4 3 4 4 3 3 3 2
output:
2 3 0 3 0 1 4
result:
ok accepted
Test #3:
score: 0
Accepted
time: 269ms
memory: 6228kb
input:
500 2 1 0 1 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1 1 1 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 1 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 0 1 0 0 ...
output:
423 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok accepted
Test #4:
score: -100
Runtime Error
input:
500 2 0 0 1 0 0 1 1 0 1 0 1 0 0 0 0 1 0 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 0 0 1 0 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0 1 0 0 0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 1 1 1 0 1 0 0 0 0 1 1 0 0 1 ...