QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#318332#7612. Matrix Inversenhuang685TL 1449ms66360kbC++208.5kb2024-01-31 05:59:552024-01-31 05:59:57

Judging History

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

  • [2024-01-31 05:59:57]
  • 评测
  • 测评结果:TL
  • 用时:1449ms
  • 内存:66360kb
  • [2024-01-31 05:59:55]
  • 提交

answer

/**
 * @file qoj7612-1.cpp
 * @author n685
 * @brief
 * @date 2024-01-29
 *
 *
 */
#include <bits/stdc++.h>

#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbgR(...) 4242
#define dbgP(...) 420
#define dbgRP(...) 420420
void nline() {}
#endif

template <class T> constexpr std::pair<T, T> exEucl(T a, T b) {
  if (a < b) {
    // auto [x, y] = exEucl(b, a);
    T x, y;
    std::tie(x, y) = exEucl(b, a);
    return {y, x};
  }
  if (b == 0) {
    assert(a == 1);
    return {1, 0};
  }
  // auto [x, y] = exEucl(b, a % b);
  T x, y;
  std::tie(x, y) = exEucl(b, a % b);
  return {y, x - (a / b) * y};
}
template <
    class T, class U,
    typename std::enable_if<std::is_integral<U>::value, bool>::type = true>
constexpr T binpow(T a, U b) {
  // 0^0 = 1
  T res = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      res *= a;
    }
    a *= a;
    b /= 2;
  }
  return res;
}

template <class Md, class V = int64_t> struct Mod {
  using T = typename std::decay<decltype(Md::value)>::type;
  T val = 0;

  template <class U> static constexpr T normalize(U val) {
    if (val <= -Md::value || Md::value <= val) {
      val %= Md::value;
    }
    if (val < 0) {
      val += Md::value;
    }
    return static_cast<T>(val);
  }

  constexpr Mod() : val(0) {}
  template <class U, typename std::enable_if<std::is_integral<U>::value,
                                             bool>::type = true>
  constexpr Mod(U _val) {
    val = normalize(_val);
  }

  // addition
  constexpr Mod &operator+=(Mod b) {
    val += b.val;
    if (val >= Md::value) {
      val -= Md::value;
    }
    return *this;
  }
  friend constexpr Mod operator+(Mod a, Mod b) { return (a += b); }
  constexpr Mod &operator++() { return (*this += 1); }
  constexpr Mod operator++(int) {
    Mod res = *this;
    ++(*this);
    return res;
  }

  // subtraction
  constexpr Mod &operator-=(Mod b) {
    val -= b.val;
    if (val < 0) {
      val += Md::value;
    }
    return *this;
  }
  friend constexpr Mod operator-(Mod a, Mod b) { return (a -= b); }
  constexpr Mod &operator--() { return (*this -= 1); }
  constexpr Mod operator--(int) {
    Mod res = *this;
    --(*this);
    return res;
  }

  // multiplication
  constexpr Mod &operator*=(Mod b) {
    val = static_cast<T>(static_cast<V>(val) * b.val % Md::value);
    return *this;
  }
  friend constexpr Mod operator*(Mod a, Mod b) { return (a *= b); }

  template <class U> constexpr Mod binpow(U b) const {
    return ::binpow(*this, b);
  }
  constexpr Mod inv() const {
    return Mod(exEucl(static_cast<V>(val), static_cast<V>(Md::value)).first);
    // return binpow(Md::value - 2);
  }

  // comparison
  constexpr bool operator==(Mod b) const { return (val == b.val); }
  // constexpr auto operator<=>(const Mod &b) const = default;
  constexpr bool operator!=(Mod b) const { return (val != b.val); }
  constexpr bool operator<(Mod b) const { return (val < b.val); }
  constexpr bool operator>(Mod b) const { return (val > b.val); }
  constexpr bool operator<=(Mod b) const { return (val <= b.val); }
  constexpr bool operator>=(Mod b) const { return (val >= b.val); }

  // io
  friend std::istream &operator>>(std::istream &in, Mod &a) {
    V v;
    in >> v;
    a = Mod(v);
    return in;
  }
  friend std::ostream &operator<<(std::ostream &out, const Mod &a) {
    out << a.val;
    return out;
  }

  // conversion
  explicit constexpr operator T() const { return val; }
  constexpr const T &operator()() const { return val; }
  constexpr Mod operator-() const { return Mod(-val); }
};

constexpr int MOD = (int)1e9 + 7;
using Mint = Mod<std::integral_constant<std::decay<decltype(MOD)>::type, MOD>>;

using Row = std::vector<Mint>;
Row operator+=(Row &a, Row b) {
  if (a.size() < b.size()) {
    a.resize(b.size());
  }
  for (int i = 0; i < (int)a.size(); ++i) {
    a[i] += b[i];
  }
  return a;
}
Row operator+(Row a, Row b) { return (a += b); }
Row operator-=(Row &a, Row b) {
  if (a.size() < b.size()) {
    a.resize(b.size());
  }
  for (int i = 0; i < (int)a.size(); ++i) {
    a[i] -= b[i];
  }
  return a;
}
Row operator-(Row a, Row b) { return (a -= b); }
Row operator*=(Row &a, Mint b) {
  for (int i = 0; i < (int)a.size(); ++i) {
    a[i] *= b;
  }
  return a;
}
Row operator*(Row a, Mint b) { return a *= b; }
Row operator*(Mint b, Row a) { return a *= b; }

using Mat = std::vector<Row>;
Row operator*(Mat a, Row b) {
  int n = (int)a.size();
  Row c(n);
  for (int i = 0; i < n; ++i) {
    for (int k = 0; k < n; ++k) {
      c[i] += a[i][k] * b[k];
    }
  }
  return c;
}
template <class T> auto operator*(std::vector<std::vector<T>> a, Mat b) {
  int n = (int)a.size(), m = (int)b[0].size();
  // Mat c(n, Row(m));
  std::vector c(n, std::vector<T>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int k = 0; k < (int)a[0].size(); ++k) {
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  return c;
}

std::vector<Mint> gauss(std::vector<Row> eq) {
  if ((int)eq.size() == 0) {
    return {};
  }
  int n = (int)eq.size();
  int m = (int)eq[0].size() - 1;
  assert(n >= m);
  for (int i = 0; i < m; ++i) {
    int ind = i;
    while (ind < n && eq[ind][i] == 0) {
      ++ind;
    }
    if (ind == n) {
      std::exit(-1);
    }
    std::swap(eq[i], eq[ind]);
    eq[i] *= eq[i][i].inv();
    for (int j = 0; j < n; ++j) {
      if (j != i) {
        eq[j] -= eq[j][i] * eq[i];
      }
    }
  }
  std::vector<Mint> sol(m);
  for (int i = 0; i < m; ++i) {
    sol[i] = eq[i].back();
  }
  return sol;
}

int main() {
#ifdef LOCAL
  std::freopen("input.txt", "r", stdin);
  std::freopen("output.txt", "w", stdout);
#else
  std::cin.tie(nullptr)->sync_with_stdio(false);
#endif

  int n;
  std::cin >> n;

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

  std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  // std::vector<int> r(n);
  // std::iota(r.begin(), r.end(), 0);
  // std::shuffle(r.begin(), r.end(), rng);
  // r.resize(std::min(n, 20));

  std::vector<int> row, col;
  {
    Row rr(n);
    for (int i = 0; i < n; ++i) {
      rr[i] = (int)(rng() % MOD);
    }
    Row rr2 = c * (a * rr);
    for (int i = 0; i < n; ++i) {
      if (rr[i] != rr2[i]) {
        row.push_back(i);
      }
    }
  }
  {
    Mat rr(1, Row(n));
    for (int i = 0; i < n; ++i) {
      rr[0][i] = (int)(rng() % MOD);
    }
    Mat rr2 = (rr * a) * c;
    for (int i = 0; i < n; ++i) {
      if (rr[0][i] != rr2[0][i]) {
        col.push_back(i);
      }
    }
  }

  const int m = (int)row.size() * (int)col.size();
  // std::vector val(n, std::vector(n, Row(m + 1)));
  // for (int i = 0; i < n; ++i) {
  //   for (int j = 0; j < n; ++j) {
  //     val[i][j].back() = c[i][j];
  //   }
  // }
  // for (int i = 0; i < (int)row.size(); ++i) {
  //   for (int j = 0; j < (int)col.size(); ++j) {
  //     val[row[i]][col[j]].back() = 0;
  //     val[row[i]][col[j]][(int)col.size() * i + j] = 1;
  //   }
  // }
  std::vector<Mint> sol(m);
  for (int i = 0; i < (int)row.size(); ++i) {
    std::vector<Row> eq;
    std::vector val(n, Row((int)col.size() + 1));
    for (int j = 0; j < n; ++j) {
      val[j].back() = c[row[i]][j];
    }
    for (int j = 0; j < (int)col.size(); ++j) {
      val[col[j]][j] = 1;
      val[col[j]].back() = 0;
    }
    std::vector res = (std::vector({val}) * a)[0];
    for (int j : col) {
      res[j].back() = (int)(row[i] == j) - res[j].back();
      eq.push_back(res[j]);
    }
    std::vector lsol = gauss(eq);
    assert(lsol.size() == col.size());
    for (int j = 0; j < (int)col.size(); ++j) {
      sol[(int)col.size() * i + j] = lsol[j];
    }
  }
  std::vector<std::pair<std::pair<int, int>, Mint>> ans;
  for (int i = 0; i < (int)row.size(); ++i) {
    for (int j = 0; j < (int)col.size(); ++j) {
      Mint v = sol[(int)col.size() * i + j];
      if (c[row[i]][col[j]] != v) {
        ans.emplace_back(std::pair{row[i], col[j]}, v);
      }
    }
  }
  std::cout << (int)ans.size() << '\n';
  for (auto [aa, b] : ans) {
    std::cout << aa.first + 1 << ' ' << aa.second + 1 << ' ' << b << '\n';
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
953176428
107682094

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Accepted
time: 952ms
memory: 66220kb

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:

2
827 238 84815305
1466 499 206940592

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 1245ms
memory: 65880kb

input:

1995
436890614 28924575 276129332 63568266 576410175 399540058 591733285 531509939 637241038 596750662 811926780 760228238 317196903 751498201 993802643 102539089 382116597 233386377 974332817 495280100 575832855 616941506 297856263 216480938 638907269 434126707 499611855 764625526 51141033 64624519...

output:

3
315 590 222982023
421 1912 523778307
745 1803 328256562

result:

ok 4 lines

Test #4:

score: 0
Accepted
time: 1449ms
memory: 66360kb

input:

1998
583111238 684686962 60000552 833067795 399706437 80311170 511421309 126675237 578609031 629890589 4721597 505178877 965431576 488092987 110903821 856966035 934194793 831090190 93501498 982251231 221658950 561834845 801921306 125139448 771849922 610370373 625334897 671223646 927123592 441019972 ...

output:

4
21 1273 160152585
700 1573 576757184
1674 1165 958703366
1860 1550 451190886

result:

ok 5 lines

Test #5:

score: -100
Time Limit Exceeded

input:

2000
717395008 183448326 460843970 942614578 540060179 334668801 284127311 635920935 518435676 579369810 852254297 342132392 390366615 141010330 256825376 585810764 253867889 483289117 141421931 467578626 750184736 801127935 917825514 702243210 954747981 910219404 311930180 11494244 915417963 820983...

output:


result: