結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー roiti46roiti46
提出日時 2017-12-20 02:32:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 3,000 ms
コード長 3,524 bytes
コンパイル時間 1,788 ms
コンパイル使用メモリ 167,588 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 07:33:01
合計ジャッジ時間 8,164 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,376 KB
testcase_01 AC 7 ms
4,384 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 5 ms
4,384 KB
testcase_06 AC 6 ms
4,376 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 6 ms
4,380 KB
testcase_11 AC 7 ms
4,376 KB
testcase_12 AC 10 ms
4,380 KB
testcase_13 AC 12 ms
4,376 KB
testcase_14 AC 19 ms
4,376 KB
testcase_15 AC 24 ms
4,380 KB
testcase_16 AC 28 ms
4,380 KB
testcase_17 AC 28 ms
4,380 KB
testcase_18 AC 36 ms
4,380 KB
testcase_19 AC 37 ms
4,380 KB
testcase_20 AC 42 ms
4,380 KB
testcase_21 AC 44 ms
4,380 KB
testcase_22 AC 51 ms
4,376 KB
testcase_23 AC 53 ms
4,376 KB
testcase_24 AC 57 ms
4,380 KB
testcase_25 AC 66 ms
4,376 KB
testcase_26 AC 64 ms
4,376 KB
testcase_27 AC 72 ms
4,384 KB
testcase_28 AC 73 ms
4,376 KB
testcase_29 AC 79 ms
4,380 KB
testcase_30 AC 75 ms
4,376 KB
testcase_31 AC 72 ms
4,380 KB
testcase_32 AC 68 ms
4,376 KB
testcase_33 AC 79 ms
4,380 KB
testcase_34 AC 75 ms
4,376 KB
testcase_35 AC 71 ms
4,380 KB
testcase_36 AC 74 ms
4,376 KB
testcase_37 AC 81 ms
4,380 KB
testcase_38 AC 83 ms
4,376 KB
testcase_39 AC 84 ms
4,384 KB
testcase_40 AC 83 ms
4,376 KB
testcase_41 AC 84 ms
4,380 KB
testcase_42 AC 82 ms
4,380 KB
testcase_43 AC 82 ms
4,376 KB
testcase_44 AC 82 ms
4,380 KB
testcase_45 AC 84 ms
4,376 KB
testcase_46 AC 82 ms
4,380 KB
testcase_47 AC 84 ms
4,380 KB
testcase_48 AC 83 ms
4,380 KB
testcase_49 AC 84 ms
4,380 KB
testcase_50 AC 83 ms
4,380 KB
testcase_51 AC 83 ms
4,380 KB
testcase_52 AC 83 ms
4,384 KB
testcase_53 AC 84 ms
4,376 KB
testcase_54 AC 83 ms
4,380 KB
testcase_55 AC 84 ms
4,376 KB
testcase_56 AC 84 ms
4,384 KB
testcase_57 AC 85 ms
4,380 KB
testcase_58 AC 71 ms
4,376 KB
testcase_59 AC 98 ms
4,380 KB
testcase_60 AC 99 ms
4,380 KB
testcase_61 AC 98 ms
4,380 KB
testcase_62 AC 99 ms
4,384 KB
testcase_63 AC 55 ms
4,376 KB
testcase_64 AC 53 ms
4,380 KB
testcase_65 AC 53 ms
4,376 KB
testcase_66 AC 54 ms
4,376 KB
testcase_67 AC 53 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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;
        res[i][j] %= mod;
      }
    }
  }
  return res;
}

// 繰り返し二乗法
Mat mat_pow(Mat m, ll p) {
  Mat res {};
  rep(i, res.size()) res[i][i] = 1;
  while (p > 0) {
    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