QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#628695 | #7612. Matrix Inverse | ucup-team4992 | WA | 583ms | 35252kb | C++14 | 6.2kb | 2024-10-10 21:45:10 | 2024-10-10 21:45:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using i64 = long long;
template <typename T>
T fpow(T x, ll n) {
T res = 1, a = x;
while (n) {
if (n & 1) res *= a;
a *= a;
n >>= 1;
}
return res;
}
const int P = 1e9 + 7;
int norm(int x) {
if (x >= P) {
x -= P;
}
if (x < 0) {
x += P;
}
return x;
}
struct ModInt {
int x;
int val() const {
return x;
}
ModInt(int _x = 0) : x(norm(_x)) {}
ModInt inv() const {
return fpow(*this, P - 2);
}
ModInt& operator+=(const ModInt& rhs) {
x = norm(x + rhs.x);
return *this;
}
ModInt& operator-=(const ModInt& rhs) {
x = norm(x - rhs.x);
return *this;
}
ModInt& operator*=(const ModInt& rhs) {
x = i64(x) * rhs.x % P;
return *this;
}
ModInt& operator/=(const ModInt& rhs) {
return *this *= rhs.inv();
}
bool operator==(const ModInt& rhs) const {
return x == rhs.x;
}
bool operator!=(const ModInt& rhs) const {
return !(x == rhs.x);
}
bool operator==(const int rhs) const {
return x == rhs;
}
bool operator!=(const int rhs) const {
return !(x == rhs);
}
friend ModInt operator+(const ModInt& a, const ModInt& b) {
ModInt res = a;
res += b;
return res;
}
friend ModInt operator-(const ModInt& a, const ModInt& b) {
ModInt res = a;
res -= b;
return res;
}
friend ModInt operator*(const ModInt& a, const ModInt& b) {
ModInt res = a;
res *= b;
return res;
}
friend ModInt operator/(const ModInt& a, const ModInt& b) {
ModInt res = a;
res /= b;
return res;
}
friend istream& operator>>(istream& is, ModInt& rhs) {
i64 v;
is >> v;
rhs = ModInt(v);
return is;
}
friend ostream& operator<<(ostream& os, const ModInt& rhs) {
os << rhs.val();
return os;
}
ModInt sqrt();
};
using Z = ModInt;
template <typename T>
struct Gauss {
vector<vector<T>> a;
int n, m;
int flag; //-1:no solution;0: only one solution; 1:multiple solution
Gauss(int _n, int _m) {
n = _n;
m = _m;
flag = 0;
a.assign(n + 1, vector<T>(m + 1));
}
void work() {
for (int col = 1, row = 1; col <= m; col++) {
for (int i = row; i <= n; i++) {
if (a[i][col].x == 0) continue;
swap(a[row], a[i]);
break;
}
if (row > n || a[row][col] == 0) continue;
T t = 1 / a[row][col];
for (int j = col; j <= m; j++)
a[row][j] *= t;
for (int i = 1; i <= n; i++) {
if (i == row || a[i][col].x == 0) continue;
t = a[i][col] / a[row][col];
for (int j = col; j <= m; j++) {
a[i][j] -= t * a[row][j];
}
}
row++;
}
flag = 0;
for (int row = 1; row <= n; row++) {
int col;
for (col = 1; col <= m; col++) {
if (a[row][col].x != 0) break;
}
if (col < m)
continue;
else if (col == m)
flag = -1;
else
flag = 1;
break;
}
}
void print() {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= m; col++) {
cout << a[row][col] << ' ';
}
cout << '\n';
}
}
};
int main() {
mt19937_64 rnd(chrono::steady_clock().now().time_since_epoch().count());
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<vector<Z>> A(n + 5, vector<Z>(n + 5)), B(A);
vector<int> iswr(n + 5), iswc(n + 5);
vector<Z> vct(n + 5);
vector<int> wr, wc;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> A[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> B[i][j];
}
}
for (int i = 1; i <= n; i++) {
vct[i] = rnd() % P;
}
vector<Z> L = vct, R = vct;
for (int i = 1; i <= n; i++) {
Z res = 0;
for (int j = 1; j <= n; j++) {
res += vct[j] * A[j][i];
}
L[i] = res;
}
for (int i = 1; i <= n; i++) {
Z res = 0;
for (int j = 1; j <= n; j++) {
res += L[j] * B[j][i];
}
if (res.x != R[i].x) {
wc.push_back(i);
iswc[i] = wc.size();
}
}
L = vct;
R = vct;
for (int i = 1; i <= n; i++) {
Z res = 0;
for (int j = 1; j <= n; j++) {
res += A[i][j] * vct[j];
}
L[i] = res;
}
for (int i = 1; i <= n; i++) {
Z res = 0;
for (int j = 1; j <= n; j++) {
res += B[i][j] * L[j];
}
if (res != R[i]) {
wr.push_back(i);
iswr[i] = wr.size();
}
}
vector<tuple<int, int, int>> ans;
for (auto col : wc) {
int k = wr.size();
Gauss<Z> gs(n, k + 1);
for (int i = 1; i <= n; i++) {
gs.a[i][k + 1] = (i == col);
for (int j = 1; j <= n; j++) {
if (iswr[j]) {
gs.a[i][iswr[j]] = A[i][j];
} else {
gs.a[i][k + 1] -= A[i][j] * B[j][col];
}
}
}
gs.print();
gs.work();
for (int i = 1; i <= k; i++) {
if (gs.a[i][k + 1].x != B[wr[i - 1]][col].x) {
ans.push_back({wr[i - 1], col, gs.a[i][k + 1].x});
}
}
}
sort(ans.begin(), ans.end());
cout << ans.size() << '\n';
for (auto [r, c, x] : ans) {
cout << r << ' ' << c << ' ' << x << '\n';
}
return 0;
}
/*
2
1 1
0 2
1 2
3 500000004
3
0 1 0
0 0 1
1 0 0
0 0 1
1 0 0
0 1 0
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3816kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Wrong Answer
time: 583ms
memory: 35252kb
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:
696227527 583800982 525340460 506577502 947027758 815017326 736584878 738198887 995963834 447452240 663804483 200831606 233070205 785651582 668944395 614951111 531190789 148857053 93635429 990629593 318726392 148410758 139339946 774267335 352846240 45919466 386103253 841196128 739290473 504...
result:
wrong answer 1st lines differ - expected: '2', found: '696227527 583800982 525340460 '