QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625613 | #7612. Matrix Inverse | ucup-team3519# | WA | 210ms | 66256kb | C++17 | 6.0kb | 2024-10-09 20:06:16 | 2024-10-09 20:06:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define V vector
template <int m>
class ModInt {
int raw;
using mint = ModInt;
using i64 = int64_t;
public:
ModInt() : raw(0) {}
ModInt(const auto &v) {
raw = v % m;
if (raw < 0) {
raw += m;
}
}
int value() const {
return raw;
}
mint &operator+=(const mint &rhs) {
raw += rhs.raw;
if (raw >= m) {
raw -= m;
}
return *this;
}
mint &operator-=(const mint &rhs) {
raw -= rhs.raw;
if (raw < 0) {
raw += m;
}
return *this;
}
mint &operator*=(const mint &rhs) {
raw = (i64)raw * rhs.raw % m;
return *this;
}
mint &operator/=(const mint &rhs) {
return *this *= qpow(rhs, m - 2);
}
friend mint operator+(const mint &lhs, const mint &rhs) {
return mint{lhs} += rhs;
}
friend mint operator-(const mint &lhs, const mint &rhs) {
return mint{lhs} -= rhs;
}
friend mint operator*(const mint &lhs, const mint &rhs) {
return mint{lhs} *= rhs;
}
friend mint operator/(const mint &lhs, const mint &rhs) {
return mint{lhs} /= rhs;
}
static constexpr int mod() {
return m;
}
static mint qpow(mint a, i64 b) {
mint res = 1;
while (b) {
if (b & 1) {
res *= a;
}
a *= a, b >>= 1;
}
return res;
}
};
using mint = ModInt<static_cast<int>(1e9 + 7)>;
constexpr int MAXN = 2e3 + 19;
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
namespace fast
{
char B[1 << 18], *S = B, *T = B;
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 18, stdin), S == T) ? 0 : *S++)
int read() {
int ret = 0;
char c;
while (c = getc(), c < '0' || c > '9');
for (; c >= '0' && c <= '9'; c = getc()) ret = ret * 10 + c - '0';
return ret;
}
}
using fast::read;
template <typename T>
std::vector<uint8_t> get_errors(int n, T buf_a, T buf_b) {
std::vector<V<mint>> a(n), b(n);
for (int i = 0; i < n; ++i) {
a[i].resize(n);
for (int j = 0; j < n; ++j) {
a[i][j] = buf_a[i][j];
}
}
for (int i = 0; i < n; ++i) {
b[i].resize(n);
for (int j = 0; j < n; ++j) {
b[i][j] = buf_b[j][i];
}
}
std::vector<mint> fac(n);
std::vector<uint8_t> fault(n);
for (int _ = 0; _ < 3; ++_) {
for (int i = 0; i < n; ++i) {
fac[i] = rng();
}
std::vector<mint> e(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
e[j] += fac[i] * a[i][j];
}
}
for (int col = 0; col < n; ++col) {
mint expected = fac[col];
mint actual = 0;
for (int i = 0; i < n; ++i) {
actual += e[i] * b[col][i];
}
if (expected.value() != actual.value()) {
fault[col] = true;
}
}
}
return fault;
}
int bufa[MAXN][MAXN], bufb[MAXN][MAXN];
void transpose(int n, auto buf) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
std::swap(buf[i][j], buf[j][i]);
}
}
}
#define pb push_back
void Gause(V<V<mint>>& eq, int m) {
int n = eq.size() - 1;
int rk = 0;
for(int now = 1; now <= m; now++) {
int j = rk + 1;
while(j <= n && eq[j][now].value() == 0) j++;
if(j == n + 1) continue;
swap(eq[j], eq[++rk]);
mint iv = 1 / eq[rk][now];
eq[rk][0] = eq[rk][0] * iv;
for(int i = now; i <= m; i++) {
if(eq[rk][i].value() != 0) {
eq[rk][i] = eq[rk][i] * iv;
}
}
for(int i = 1; i <= n; i++) {
if(eq[i][now].value() == 0 || i == rk) continue;
eq[i][0] -= eq[rk][0] * eq[i][now];
for(int j = m; j >= now; j--) {
eq[i][j] -= eq[rk][j] * eq[i][now];
}
}
}
}
int main() {
int n = read();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
bufa[i][j] = read();
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
bufb[i][j] = read();
}
}
transpose(n, bufa);
transpose(n, bufb);
auto row = get_errors(n, bufa, bufb);
transpose(n, bufa);
transpose(n, bufb);
V<uint8_t> col = get_errors(n, bufa, bufb);
V<int> row_pos, col_pos;
for(int i = 0; i < n; i++) {
if(row[i]) row_pos.pb(i);
}
for(int i = 0; i < n; i++) {
if(col[i]) col_pos.pb(i);
}
V<array<int, 3>> ans;
for(auto j : col_pos) {
V<V<mint>> eq(1);
V<mint> C(n);
for(int i = 0; i < n; i++) {
for(int k = 0; k < n; k++) {
C[i] += bufa[i][k] * bufb[k][j];
}
}
for(int k = 0; k < n; k++) {
C[k] = (j == k ? 1 : 0) - C[k];
}
for(int i = 0; i < n; i++) {
V<mint> now;
now.pb(C[i]);
for(auto y : row_pos) {
now.pb(bufa[i][y]);
}
eq.pb(now);
}
Gause(eq, row_pos.size());
for(int i = 1; i <= row_pos.size(); i++) {
if(eq[i][0].value() != 0) {
ans.pb({row_pos[i - 1], j, bufb[row_pos[i - 1]][j] + eq[i][0].value()});
}
}
}
cout << ans.size() << endl;
sort(ans.begin(), ans.end());
for(auto x : ans) {
// x[2] = -x[2];
int mod = 1e9 + 7;
if(x[2] >= mod) x[2] -= 1e9 + 7;
cout << x[0] + 1 << " " << x[1] + 1 << " " << x[2] << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7940kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 210ms
memory: 66256kb
input:
1995 586309310 548144807 578573993 437893403 641164340 712256053 172321263 108058526 768610920 123320669 762746291 856047593 979279376 29067913 309867338 292286426 45124325 239705174 675003623 213743652 620561338 116308277 695369179 669459894 682522334 846995555 159510341 999359657 645579085 7499563...
output:
4 827 238 198486459 827 499 892378306 1466 238 12279097 1466 499 872365367
result:
wrong answer 1st lines differ - expected: '2', found: '4'