結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー parukiparuki
提出日時 2017-12-21 21:55:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 3,000 ms
コード長 2,772 bytes
コンパイル時間 2,240 ms
コンパイル使用メモリ 174,404 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 20:53:53
合計ジャッジ時間 10,111 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,380 KB
testcase_01 AC 11 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 7 ms
4,376 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 9 ms
4,376 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 9 ms
4,376 KB
testcase_11 AC 9 ms
4,376 KB
testcase_12 AC 15 ms
4,376 KB
testcase_13 AC 18 ms
4,376 KB
testcase_14 AC 27 ms
4,380 KB
testcase_15 AC 35 ms
4,376 KB
testcase_16 AC 41 ms
4,376 KB
testcase_17 AC 42 ms
4,376 KB
testcase_18 AC 54 ms
4,376 KB
testcase_19 AC 55 ms
4,380 KB
testcase_20 AC 63 ms
4,384 KB
testcase_21 AC 66 ms
4,380 KB
testcase_22 AC 77 ms
4,380 KB
testcase_23 AC 79 ms
4,380 KB
testcase_24 AC 85 ms
4,376 KB
testcase_25 AC 99 ms
4,380 KB
testcase_26 AC 97 ms
4,376 KB
testcase_27 AC 110 ms
4,376 KB
testcase_28 AC 111 ms
4,380 KB
testcase_29 AC 119 ms
4,376 KB
testcase_30 AC 112 ms
4,376 KB
testcase_31 AC 110 ms
4,376 KB
testcase_32 AC 103 ms
4,376 KB
testcase_33 AC 117 ms
4,380 KB
testcase_34 AC 113 ms
4,376 KB
testcase_35 AC 107 ms
4,376 KB
testcase_36 AC 111 ms
4,380 KB
testcase_37 AC 121 ms
4,380 KB
testcase_38 AC 124 ms
4,380 KB
testcase_39 AC 125 ms
4,384 KB
testcase_40 AC 126 ms
4,376 KB
testcase_41 AC 127 ms
4,380 KB
testcase_42 AC 123 ms
4,380 KB
testcase_43 AC 124 ms
4,376 KB
testcase_44 AC 124 ms
4,376 KB
testcase_45 AC 125 ms
4,380 KB
testcase_46 AC 124 ms
4,376 KB
testcase_47 AC 125 ms
4,376 KB
testcase_48 AC 126 ms
4,376 KB
testcase_49 AC 126 ms
4,376 KB
testcase_50 AC 124 ms
4,384 KB
testcase_51 AC 125 ms
4,380 KB
testcase_52 AC 125 ms
4,376 KB
testcase_53 AC 126 ms
4,380 KB
testcase_54 AC 125 ms
4,376 KB
testcase_55 AC 126 ms
4,376 KB
testcase_56 AC 126 ms
4,376 KB
testcase_57 AC 128 ms
4,380 KB
testcase_58 AC 107 ms
4,380 KB
testcase_59 AC 146 ms
4,376 KB
testcase_60 AC 148 ms
4,376 KB
testcase_61 AC 147 ms
4,380 KB
testcase_62 AC 148 ms
4,376 KB
testcase_63 AC 78 ms
4,376 KB
testcase_64 AC 79 ms
4,376 KB
testcase_65 AC 79 ms
4,376 KB
testcase_66 AC 80 ms
4,376 KB
testcase_67 AC 80 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

const int MOD = (int)1e9 + 7;

/*
行列の積(mod)
行列Aと行列Bの積を返す
O(n^3)
*/
vector<vector<int> > mulMatMod(const vector<vector<int> > &A, const vector<vector<int> > &B) {
    auto n = A.size(), m = A[0].size(), p = B[0].size();
    vector<vector<int> > res(n, vector<int>(p));
    for (int i = 0; i < n; ++i)for (int j = 0; j < p; ++j) for (int k = 0; k < m; ++k)
        res[i][j] = (int)((res[i][j] + (long long)A[i][k] * B[k][j]) % MOD);
    return res;
}

/*
行列Aの累乗(mod)を返す
O(n^3 * log(k))
*/
vector<vector<int> > powMatMod(vector<vector<int> > A, long long k) {
    auto n = A.size();
    vector<vector<int> > res(n, vector<int>(n));
    for (int i = 0; i < n; ++i)res[i][i] = 1;
    while (k) {
        if (k % 2)res = mulMatMod(res, A);
        k /= 2;
        A = mulMatMod(A, A);
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    const int R = 1 << 6;
    const int START = 0b111;
    const int GOAL[] = {
        0b111,0b011,0b101,0b010,0b110
    };

    ll N;
    int a[3][3];
    cin >> N;
    vi D[3] = { {},{0,0,1,0},{0,0,0,1} };

    vector<vi> A(R, vi(R));

    rep(S, R) {
        rep(i, 3)rep(j, 3)rep(k, 2) {
            MEM(a, 0);
            rep(l, 6) {
                int x = l / 3, y = l % 3;
                a[x][y] = S >> l & 1;
            }
            int ok = 1, d[] = { i,j,k };
            rep(l, 3) {
                for (int m = 0; m < sz(D[d[l]]); m += 2) {
                    int x = 1 + D[d[l]][m];
                    int y = l + D[d[l]][m + 1];
                    if (a[x][y])ok = 0;
                    else a[x][y] = 1;
                }
            }
            
            rep(y, 3) {
                if (!a[0][y] && !a[1][y])ok = 0;
                if (y < 2 && !a[0][y] && !a[0][y + 1])ok = 0;
            }

            int T = 0;
            rep(l, 6) {
                int x = l / 3 + 1, y = l % 3;
                if (a[x][y])T |= 1 << l;
            }

            if(ok)A[T][S]++;
        }
    }

    A = powMatMod(A, N);

    vector<vi> v(R, vi(1));
    v[START][0] = 1;
    
    v = mulMatMod(A, v);

    ll ans = 0;
    rep(i, 5)ans += v[GOAL[i]][0];

    cout << ans%MOD << endl;
}
0