結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー PachicobuePachicobue
提出日時 2017-12-21 23:44:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 3,000 ms
コード長 4,368 bytes
コンパイル時間 2,213 ms
コンパイル使用メモリ 204,804 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-10 02:40:57
合計ジャッジ時間 4,611 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T, std::size_t n>
ostream& operator<<(ostream& os, const array<T, n>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;

constexpr int N = 31;

struct Vec {
    Vec()
    {
        for (int i = 0; i < N; i++) {
            table[i] = 0;
        }
    }

    Vec(const string& s)
    {
        assert(s.size() == N);
        for (int i = 0; i < N; i++) {
            table[i] = s[i] - '0';
        }
    }
    ll operator*(const Vec& vec) const
    {
        ll ans = 0;
        for (int i = 0; i < N; i++) {
            ans += table[i] * vec.table[i];
            ans %= MOD;
        }
        return ans;
    }

    array<ll, N> table;
};

struct Matrix {
    Matrix()
    {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                table[i][j] = 0;
            }
        }
    }

    Matrix(const array<string, N>& s)
    {
        for (int i = 0; i < N; i++) {
            assert(s[i].size() == N);
            for (int j = 0; j < N; j++) {
                table[i][j] = s[i][j] - '0';
            }
        }
    }

    static Matrix Unit()
    {
        Matrix ans;
        for (int i = 0; i < N; i++) {
            ans.table[i][i] = 1;
        }
        return ans;
    }

    Matrix operator*(const Matrix& mat) const
    {
        Matrix ans;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                for (int k = 0; k < N; k++) {
                    ans.table[i][j] += table[i][k] * mat.table[k][j];
                    ans.table[i][j] %= MOD;
                }
            }
        }
        return ans;
    }

    array<array<ll, N>, N> table;
};

inline Vec operator*(const Vec& vec, const Matrix& mat)
{
    Vec ans;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            ans.table[i] += mat.table[j][i] * vec.table[j];
            ans.table[i] %= MOD;
        }
    }
    return ans;
}

Matrix power(const Matrix& mat, const ll n)
{
    if (n == 0) {
        return Matrix::Unit();
    }
    if (n % 2 == 1) {
        return mat * power(mat, n - 1);
    } else {
        const Matrix pp = power(mat, n / 2);
        return pp * pp;
    }
}

int main()
{

    cin.tie(0);
    ios::sync_with_stdio(false);

    const array<string, N> trans{{
        //0123456789012345678901234567890
        "0000000000000000000010000000001",
        "0000000010000010000000000100001",
        "0000000000000000000010000100001",
        "0000000000000010000010000000001",
        "0000000010000010000010000100001",
        "0000000000000000001000000010100",
        "0000000001000000000000000010000",
        "0000000001000000001000000010100",
        "0000000000000000000001000000000",
        "0000000000000000010001000000100",
        "0000010000000000000001000001000",
        "1000010000001000010001100001010",
        "0000000000000000000100000000001",
        "0010000000000010000100001000001",
        "0000000000000000000100000000001",
        "0010000000000010000100001000001",
        "0010000000000010000100001000001",
        "0000001000000000100000000000100",
        "0000000000100000100000000000000",
        "0000001000100000100000000000100",
        "0000000000010000000000000000000",
        "0100000000010100000000010000000",
        "0000000100000000000000000000001",
        "0001000100000001000000000100001",
        "0001000100000001000000000100001",
        "0000000100000000000000000000001",
        "0001000100000001000000000100001",
        "0000100000000000100000000010000",
        "0000100000000000100000000010000",
        "0000100000000000100000000010000",
        "0000100000000000100000000010000",
    }};
    //0123456789012345678901234567890
    const string init = "0011000110000021000110001200003";
    const string proper = "0000000000010101100001011011111";

    const Matrix mat(trans);
    const Vec initial(init);
    const Vec last(proper);
    ll n;
    cin >> n;
    if (n == 1) {
        cout << 2 << endl;
    } else {
        const Vec ans = initial * power(mat, n - 2);
        show(ans.table);
        cout << ans * last << endl;
    }

    return 0;
}
0