結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー roiti46roiti46
提出日時 2017-12-20 02:21:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,500 bytes
コンパイル時間 1,503 ms
コンパイル使用メモリ 165,584 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-22 07:32:36
合計ジャッジ時間 7,673 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,376 KB
testcase_01 AC 7 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;
using ll = long long;
using Mat = array<array<ll, 64>, 64>;

const ll mod = (ll)1e9 + 7;

ll N;

Mat mat_mul(const Mat &m1, const Mat &m2) {
  Mat res {};
  rep(i, 64) {
    rep(k, 64) {
      rep(j, 64) {
        res[i][j] += (m1[i][k] * m2[k][j] % mod + mod) % mod;
      }
    }
  }
  return res;
}

// 繰り返し二乗法
Mat mat_pow(Mat m, ll p) {
  Mat res {};
  rep(i, res.size()) res[i][i] = 1;
  while (p) {
    if (p & 1) res = mat_mul(res, m);
    m = mat_mul(m, m);
    p /= 2;
  }
  return res;
}

int main(void) {
  cin >> N;

  // 状態遷移を表す行列(mat[今の状態][次の状態] = 遷移数)
  Mat mat {};
  
  // 今の列と1つ左の列の状態から次の状態への遷移数を数え上げる
  rep(from, 0b111111 + 1) {
    // マスにブロックがあるか(今の列の上→下、左の列の上→下)
    // 左の列の状態は変えずに、今の列にどうブロックを置くかを考える
    bool r0 = (from&0b000001) != 0;
    bool r1 = (from&0b000010) != 0;
    bool r2 = (from&0b000100) != 0;
    bool l0 = (from&0b001000) != 0;
    bool l1 = (from&0b010000) != 0;
    bool l2 = (from&0b100000) != 0;

    // --- ブロックを横向きに置く場合 ---
    rep(p, 0b111 + 1) {
      // trueの行に横向きにブロックを置きたい
      bool p0 = (p&0b001) != 0;
      bool p1 = (p&0b010) != 0;
      bool p2 = (p&0b100) != 0;

      // 置けないパターン
      if (r0 && p0 || r1 && p1 || r2 && p2) continue;  

      // 置かないといけない場所に置かないパターン
      if (!l0 && !p0 || !l1 && !p1 || !l2 && !p2) continue;
      if (!(r0 | p0) && !(r1 | p1)) continue;
      if (!(r2 | p2) && !(r1 | p1)) continue;

      int r = (r2 << 2) + (r1 << 1) + r0;
      int to = ((p | r) << 3) | p;
      mat[from][to]++;
    }

    // --- ブロックを縦向きに置く場合 ---
    // 今の列の上2つに空きがある
    if (!r0 && !r1) {
      if (r2) {
        int to = (0b111 << 3) | 0b000;  // r0r1にブロックを置く
        mat[from][to]++;
      }
      else if (l2) {
        int to;
        to = (0b011 << 3) | 0b000;  // r0r1にブロックを置く
        mat[from][to]++;
        to = (0b111 << 3) | 0b100;  // r0r1に加え、r2に横向きに置く 
        mat[from][to]++;
      }
      else {
        int to = (0b111 << 3) | 0b100;  // r0r1に加え、r2に横向きに置く
        mat[from][to]++;
      }
    }

    // 今の列の下2つに空きがある
    if (!r1 && !r2) {
      if (r0) {
        int to = (0b111 << 3) | 0b000;  // r1r2にブロックを置く
        mat[from][to]++;
      }
      else if (l0) {
        int to;
        to = (0b110 << 3) | 0b000;  // r1r2にブロックを置く
        mat[from][to]++;
        to = (0b111 << 3) | 0b001;  // r1r2に加え、r0に横向きに置く
        mat[from][to]++;
      }
      else {
        int to = (0b111 << 3) | 0b001;  // r1r2に加え、r0に横向きに置
        mat[from][to]++;
      }
    }
  }

  // 繰り返し二乗法で終状態での遷移数を計算
  mat = mat_pow(mat, N);

  ll ans = 0;
  rep(j, 0b111 + 1) {
    if (j == 0b000 || j == 0b001 || j == 0b100) continue;  // ブロックをまだ置けるので終状態ではない
    int end = j << 3;
    ans += mat[0b111000][end];  // 初期状態から終状態への遷移数
    ans %= mod;
  }
  cout << ans << endl;
}
0