QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#226997 | #7615. Sequence Folding | ckiseki | RE | 0ms | 0kb | C++20 | 4.6kb | 2023-10-26 19:38:30 | 2023-10-26 19:38:30 |
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(const char *s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(const char *s, auto L, auto R) {
cerr << "\e[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++f)
cerr << (f ? ", " : "") << *L++;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
const int mod = 1000000007;
const int maxn = 2025;
int A[maxn][maxn];
int C[maxn][maxn];
mt19937 rng;
int modadd(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
int modsub(int a, int b) {
return a - b < 0 ? a - b + mod : a - b;
}
int modmul(int64_t a, int64_t b) {
return static_cast<int>(a * b % mod);
}
int modpow(int e, int p) {
int r = 1;
while (p) {
if (p & 1) r = modmul(r, e);
e = modmul(e, e);
p >>= 1;
}
return r;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> A[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> C[i][j];
}
}
vector<int> nonzero_row(n);
vector<int> nonzero_col(n);
vector<int> x(n), y(n), z(n);
for (int it = 0; it < 3; it++) {
for (int j = 0; j < n; j++) {
x[j] = rng() % mod;
}
fill(all(y), 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
y[i] = modadd(y[i], modmul(A[i][j], x[j]));
}
}
fill(all(z), 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
z[i] = modadd(z[i], modmul(C[i][j], y[j]));
}
}
for (int i = 0; i < n; i++)
z[i] = modadd(z[i], mod - x[i]);
for (int i = 0; i < n; i++)
if (z[i] != 0)
nonzero_row[i] = true;
}
for (int it = 0; it < 3; it++) {
for (int j = 0; j < n; j++) {
x[j] = rng() % mod;
}
fill(all(y), 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
y[i] = modadd(y[i], modmul(C[j][i], x[j]));
}
}
fill(all(z), 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
z[i] = modadd(z[i], modmul(A[j][i], y[j]));
}
}
for (int i = 0; i < n; i++)
z[i] = modadd(z[i], mod - x[i]);
for (int i = 0; i < n; i++)
if (z[i] != 0)
nonzero_col[i] = true;
}
orange(all(nonzero_row));
orange(all(nonzero_col));
vector<int> row_index(n, -1), col_index(n, -1);
vector<int> row, col;
for (int i = 0; i < n; i++)
if (nonzero_row[i]) {
row_index[i] = row.size();
row.push_back(i);
}
for (int i = 0; i < n; i++)
if (nonzero_col[i]) {
col_index[i] = col.size();
col.push_back(i);
}
vector<tuple<int,int,int>> ans;
for (int j : col) {
debug(j);
vector<vector<int>> AA;
vector<int> bb;
for (int i = 0; i < n; i++) {
vector<int> a(row.size());
int coef = (i == j);
for (int k = 0; k < n; k++) {
if (nonzero_row[k]) {
a[row_index[k]] = A[i][k];
} else {
coef = modsub(coef, modmul(A[i][k], C[k][j]));
}
}
AA.push_back(a);
bb.push_back(coef);
}
for (int v = 0; v < (int)AA.size(); v++) {
orange(all(AA[v]));
debug(v, bb[v]);
}
for (int v = 0; v < (int)row.size(); v++) {
int pivot = v;
for (int i = v; i < n; i++) {
if (AA[i][v] != 0) {
pivot = i;
break;
}
}
swap(AA[v], AA[pivot]);
swap(bb[v], bb[pivot]);
assert (AA[v][v] != 0);
const int inv = modpow(AA[v][v], mod - 2);
for (int k = 0; k < (int)row.size(); k++) {
AA[v][k] = modmul(AA[v][k], inv);
}
bb[v] = modmul(bb[v], inv);
for (int i = 0; i < n; i++) {
if (i == v) continue;
const int r = mod - AA[i][v];
for (int k = 0; k < (int)row.size(); k++)
AA[i][k] = modadd(AA[i][k], modmul(AA[v][k], r));
bb[i] = modadd(bb[i], modmul(bb[v], r));
}
}
for (int v = 0; v < (int)row.size(); v++) {
orange(all(AA[v]));
debug(bb[v]);
if (C[row[v]][j] != bb[v]) {
ans.emplace_back(row[v], j, bb[v]);
}
}
}
cout << ans.size() << '\n';
for (auto [a, b, c] : ans)
cout << a + 1 << ' ' << b + 1 << ' ' << c << '\n';
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
8 3 1 5 8