結果

問題 No.2405 Minimal Matrix Decomposition
ユーザー SSRSSSRS
提出日時 2023-08-04 22:11:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,920 ms / 2,000 ms
コード長 3,971 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 175,104 KB
実行使用メモリ 8,520 KB
最終ジャッジ日時 2024-04-22 20:27:55
合計ジャッジ時間 16,478 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 58 ms
8,520 KB
testcase_05 AC 1,920 ms
8,516 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 665 ms
6,760 KB
testcase_11 AC 891 ms
7,624 KB
testcase_12 AC 11 ms
5,376 KB
testcase_13 AC 135 ms
5,376 KB
testcase_14 AC 163 ms
5,376 KB
testcase_15 AC 81 ms
5,376 KB
testcase_16 AC 40 ms
5,376 KB
testcase_17 AC 285 ms
5,376 KB
testcase_18 AC 271 ms
5,376 KB
testcase_19 AC 329 ms
5,504 KB
testcase_20 AC 11 ms
5,376 KB
testcase_21 AC 39 ms
5,376 KB
testcase_22 AC 650 ms
6,916 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 9 ms
5,376 KB
testcase_25 AC 38 ms
5,376 KB
testcase_26 AC 473 ms
6,196 KB
testcase_27 AC 100 ms
5,376 KB
testcase_28 AC 563 ms
6,000 KB
testcase_29 AC 172 ms
5,376 KB
testcase_30 AC 548 ms
6,548 KB
testcase_31 AC 160 ms
5,376 KB
testcase_32 AC 27 ms
5,376 KB
testcase_33 AC 389 ms
5,780 KB
testcase_34 AC 52 ms
5,376 KB
testcase_35 AC 3 ms
5,376 KB
testcase_36 AC 129 ms
5,376 KB
testcase_37 AC 199 ms
5,376 KB
testcase_38 AC 372 ms
5,596 KB
testcase_39 AC 632 ms
6,040 KB
testcase_40 AC 193 ms
5,716 KB
testcase_41 AC 14 ms
5,376 KB
testcase_42 AC 535 ms
6,424 KB
testcase_43 AC 99 ms
5,376 KB
testcase_44 AC 92 ms
5,376 KB
testcase_45 AC 30 ms
5,376 KB
testcase_46 AC 104 ms
5,376 KB
testcase_47 AC 120 ms
5,376 KB
testcase_48 AC 33 ms
5,376 KB
testcase_49 AC 10 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int MOD;
  cin >> MOD;
  int N, M;
  cin >> N >> M;
  auto modpow = [&](long long a, long long b){
    long long ans = 1;
    while (b > 0){
      if (b % 2 == 1){
        ans *= a;
        ans %= MOD;
      }
      a *= a;
      a %= MOD;
      b /= 2;
    }
    return ans;
  };
  auto modinv = [&](long long a){
    return modpow(a, MOD - 2);
  };
  vector<vector<long long>> A(N, vector<long long>(M));
  for (int i = 0; i < N; i++){
    for (int j = 0; j < M; j++){
      cin >> A[i][j];
    }
  }
  if (A == vector<vector<long long>>(N, vector<long long>(M, 0))){
    if (N + M < N * M){
      cout << 2 << endl;
      cout << N << ' ' << 1 << endl;
      for (int i = 0; i < N; i++){
        cout << 0 << endl;
      }
      cout << 1 << ' ' << M << endl;
      for (int i = 0; i < M; i++){
        cout << 0;
        if (i < M - 1){
          cout << ' ';
        }
      }
      cout << endl;
    } else {
      cout << 1 << endl;
      cout << N << ' ' << M << endl;
      for (int i = 0; i < N; i++){
        for (int j = 0; j < M; j++){
          cout << 0;
          if (j < M - 1){
            cout << ' ';
          }
        }
        cout << endl;
      }
    }
  } else {
    vector<vector<long long>> A2 = A;
    vector<vector<long long>> P(N, vector<long long>(N, 0));
    for (int i = 0; i < N; i++){
      P[i][i] = 1;
    }
    vector<vector<long long>> Q(M, vector<long long>(M, 0));
    for (int i = 0; i < M; i++){
      Q[i][i] = 1;
    }
    int rank = 0;
    for (int i = 0; i < N; i++){
      int x = -1, y = -1;
      for (int j = i; j < N; j++){
        for (int k = i; k < M; k++){
          if (A[j][k] != 0){
            x = j;
            y = k;
          }
        }
      }
      if (x == -1 && y == -1){
        break;
      }
      rank++;
      if (x != i){
        for (int j = 0; j < N; j++){
          swap(P[j][x], P[j][i]);
        }
        for (int j = 0; j < M; j++){
          swap(A[x][j], A[i][j]);
        }
      }
      if (y != i){
        for (int j = 0; j < M; j++){
          swap(Q[y][j], Q[i][j]);
        }
        for (int j = 0; j < N; j++){
          swap(A[j][i], A[j][y]);
        }
      }
      long long m = modinv(A[i][i]);
      for (int j = 0; j < N; j++){
        P[j][i] *= A[i][i];
        P[j][i] %= MOD;
      }
      for (int j = 0; j < M; j++){
        A[i][j] *= m;
        A[i][j] %= MOD;
      }
      for (int j = i + 1; j < N; j++){
        long long m2 = A[j][i];
        for (int k = 0; k < N; k++){
          P[k][i] += P[k][j] * m2;
          P[k][i] %= MOD;
        }
        for (int k = 0; k < M; k++){
          A[j][k] -= A[i][k] * m2 % MOD;
          if (A[j][k] < 0){
            A[j][k] += MOD;
          }
        }
      }
      for (int j = i + 1; j < M; j++){
        long long m2 = A[i][j];
        for (int k = 0; k < M; k++){
          Q[i][k] += Q[j][k] * m2;
          Q[i][k] %= MOD;
        }
        for (int k = 0; k < N; k++){
          A[k][j] -= A[k][i] * m2 % MOD;
          if (A[k][j] < 0){
            A[k][j] += MOD;
          }
        }
      }
    }
    if (rank * (N + M) < N * M){
      cout << 2 << endl;
      cout << N << ' ' << rank << endl;
      for (int i = 0; i < N; i++){
        for (int j = 0; j < rank; j++){
          cout << P[i][j];
          if (j < rank - 1){
            cout << ' ';
          }
        }
        cout << endl;
      }
      cout << rank << ' ' << M << endl;
      for (int i = 0; i < rank; i++){
        for (int j = 0; j < M; j++){
          cout << Q[i][j];
          if (j < M - 1){
            cout << ' ';
          }
        }
        cout << endl;
      }
    } else {
      cout << 1 << endl;
      cout << N << ' ' << M << endl;
      for (int i = 0; i < N; i++){
        for (int j = 0; j < M; j++){
          cout << A2[i][j];
          if (j < M - 1){
            cout << ' ';
          }
        }
        cout << endl;
      }
    }
  }
}
0