結果

問題 No.2405 Minimal Matrix Decomposition
ユーザー SSRSSSRS
提出日時 2023-08-04 22:09:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,911 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 175,668 KB
実行使用メモリ 8,648 KB
最終ジャッジ日時 2024-04-22 20:25:53
合計ジャッジ時間 6,848 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 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 59 ms
8,516 KB
testcase_05 TLE -
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 59 ms
6,940 KB
testcase_10 AC 1,097 ms
6,940 KB
testcase_11 AC 1,471 ms
7,624 KB
testcase_12 AC 16 ms
6,940 KB
testcase_13 AC 226 ms
6,948 KB
testcase_14 AC 276 ms
6,944 KB
testcase_15 AC 146 ms
6,944 KB
testcase_16 AC 61 ms
6,944 KB
testcase_17 AC 476 ms
6,940 KB
testcase_18 AC 459 ms
6,944 KB
testcase_19 AC 531 ms
6,944 KB
testcase_20 AC 16 ms
6,944 KB
testcase_21 AC 62 ms
6,944 KB
testcase_22 AC 1,056 ms
6,944 KB
testcase_23 AC 5 ms
6,944 KB
testcase_24 AC 11 ms
6,944 KB
testcase_25 AC 63 ms
6,944 KB
testcase_26 AC 773 ms
6,948 KB
testcase_27 AC 161 ms
6,940 KB
testcase_28 AC 921 ms
6,944 KB
testcase_29 AC 281 ms
6,940 KB
testcase_30 AC 916 ms
6,944 KB
testcase_31 AC 262 ms
6,944 KB
testcase_32 AC 42 ms
6,944 KB
testcase_33 AC 649 ms
6,944 KB
testcase_34 AC 88 ms
6,944 KB
testcase_35 AC 3 ms
6,944 KB
testcase_36 AC 233 ms
6,940 KB
testcase_37 AC 318 ms
6,940 KB
testcase_38 AC 611 ms
6,940 KB
testcase_39 AC 983 ms
6,944 KB
testcase_40 AC 309 ms
6,944 KB
testcase_41 AC 20 ms
6,944 KB
testcase_42 AC 856 ms
6,940 KB
testcase_43 AC 159 ms
6,944 KB
testcase_44 AC 147 ms
6,948 KB
testcase_45 AC 47 ms
6,944 KB
testcase_46 AC 174 ms
6,948 KB
testcase_47 AC 191 ms
6,940 KB
testcase_48 AC 49 ms
6,940 KB
testcase_49 AC 15 ms
6,944 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 % MOD;
          P[k][i] %= MOD;
        }
        for (int k = 0; k < M; k++){
          A[j][k] += MOD - A[i][k] * m2 % MOD;
          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 % MOD;
          Q[i][k] %= MOD;
        }
        for (int k = 0; k < N; k++){
          A[k][j] += MOD - A[k][i] * m2 % MOD;
          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