QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#425366#7612. Matrix InversedayuxWA 668ms81464kbC++143.2kb2024-05-30 09:44:252024-05-30 09:44:25

Judging History

你现在查看的是最新测评结果

  • [2024-05-30 09:44:25]
  • 评测
  • 测评结果:WA
  • 用时:668ms
  • 内存:81464kb
  • [2024-05-30 09:44:25]
  • 提交

answer

#include <bits/stdc++.h>

const int mod = 1e9 + 7;

int qpow(int x, int n = mod - 2) {
  int ret = 1;
  while (n > 0) {
    if (n & 1) {
      ret = (long long)ret * x % mod;
    }
    x = (long long)x * x % mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  std::cin.tie(nullptr)->sync_with_stdio(false);

  int n;
  std::cin >> n;
  std::vector<std::vector<int>> a(n + 1, std::vector<int>(n + 1));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      std::cin >> a[i][j];
    }
  }
  std::vector<std::vector<int>> c(n + 1, std::vector<int>(n + 1));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      std::cin >> c[i][j];
    }
  }

  std::vector<int> cr, wr;
  {
    std::vector<int> vec(n + 1);
    std::iota(vec.data() + 1, vec.data() + n + 1, 1);
    for (auto m : {a, c}) {
      std::vector<int> res(n + 1, 0);
      for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
          res[i] = (res[i] + (long long)m[i][j] * vec[j]) % mod;
        }
      }
      vec = std::move(res);
    }
    for (int i = 1; i <= n; ++i) {
      (vec[i] == i ? cr : wr).push_back(i);
    }
  }

  std::vector<int> cc, wc;
  {
    std::vector<int> vec(n + 1);
    std::iota(vec.data() + 1, vec.data() + n + 1, 1);
    for (auto m : {a, c}) {
      std::vector<int> res(n + 1, 0);
      for (int j = 1; j <= n; ++j) {
        for (int i = 1; i <= n; ++i) {
          res[j] = (res[j] + (long long)vec[i] * m[i][j]) % mod;
        }
      }
      vec = std::move(res);
    }
    for (int i = 1; i <= n; ++i) {
      (vec[i] == i ? cc : wc).push_back(i);
    }
  }

  std::vector<std::vector<int>> b(n + 1, std::vector<int>(n + 1));
  for (int i : cr) {
    for (int j = 1; j <= n; ++j) {
      b[i][j] = c[i][j];
    }
  }
  for (int i : wr) {
    for (int j : cc) {
      b[i][j] = c[i][j];
    }
  }

  for (int r : wr) {
    std::vector<std::vector<int>> e(n + 1, std::vector<int>(wc.size() + 1));
    for (int i = 1; i <= n; ++i) {
      e[i][wc.size()] = i == r;
      for (int j : cc) {
        e[i][wc.size()] = (e[i][wc.size()] - (long long)b[r][j] * a[j][i]) % mod;
      }
      for (int j = 0; j < (int)wc.size(); ++j) {
        e[i][j] = a[wc[j]][i];
      }
    }

    for (int i = 0; i < (int)wc.size(); ++i) {
      for (int j = i + 1; j <= n; ++j) {
        if (e[j][i] != 0) {
          std::swap(e[i + 1], e[j]);
          break;
        }
      }

      int inv = qpow(e[i + 1][i]);
      for (int j = 0; j <= (int)wc.size(); ++j) {
        e[i + 1][j] = (long long)e[i + 1][j] * inv % mod;
      }
      for (int j = 1; j <= n; ++j) {
        if (j != i + 1) {
          int coef = e[j][i];
          for (int k = 0; k <= (int)wc.size(); ++k) {
            e[j][k] = (e[j][k] - (long long)coef * e[i + 1][k]) % mod;
          }
        }
      }
    }
    for (int i = 0; i < (int)wc.size(); ++i) {
      b[r][wc[i]] = e[i + 1][wc.size()];
    }
  }

  std::vector<std::tuple<int, int, int>> vres;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (b[i][j] != c[i][j]) {
        vres.emplace_back(i, j, b[i][j] < 0 ? b[i][j] + mod : b[i][j]);
      }
    }
  }

  std::cout << vres.size() << "\n";
  for (auto [x, y, v] : vres) {
    std::cout << x << " " << y << " " << v << "\n";
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3648kb

input:

1
953176428
107682094

output:

0

result:

ok single line: '0'

Test #2:

score: -100
Wrong Answer
time: 668ms
memory: 81464kb

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:

3
827 238 84815305
827 499 451110062
1466 499 206940592

result:

wrong answer 1st lines differ - expected: '2', found: '3'