結果

問題 No.1073 無限すごろく
ユーザー hedwig100hedwig100
提出日時 2020-06-05 23:25:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,914 bytes
コンパイル時間 1,910 ms
コンパイル使用メモリ 171,676 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 17:59:34
合計ジャッジ時間 2,938 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i ++)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
using Matrix = vector<vector<ll>>;

Matrix mat_mul(Matrix &A,Matrix &B,int mod = MOD) {
    Matrix ans(A.size(),vector<ll>(B[0].size()));
    for (int i = 0;i < A.size(); i++) {
        for (int j = 0;j < A[0].size();j ++) {
            for (int k = 0;k < A[0].size(); k++) {
                ans[i][j] += A[i][k]*B[k][j];
                ans[i][j] %= MOD;
            }
        }
    }
    return ans;
}

Matrix mat_pow(Matrix &A,long long N,int mod = MOD) {
    Matrix ans(A.size(),vector<ll>(A.size()));
    rep(i,A.size()) {
        rep(j,A.size()) {
            if (i == j) ans[i][j] = 1;
            else ans[i][j] = 0;
        }
    }
    Matrix X = A;
    while (N > 0) {
        if (N&1) ans = mat_mul(ans,X);
        N >>= 1;
        X = mat_mul(X,X);
    }
    return ans;
}

long long modpow(ll N,ll K,ll mod = MOD) {
    long long ret = 1;
    while (K > 0) {
        if (K&1) {
            ret *= N;
            ret %= mod;
        }
        K >>= 1;
        N *= N;
        N %= mod;
    }
    return ret;
}

int main() {
    ll N; cin >> N;
    ll one_sixth = modpow(6,MOD - 2,MOD);

    Matrix A(6,vector<ll>(6));
    rep(i,6) A[0][i] = one_sixth;
    for (int i = 1;i < 6;i++) A[i][i - 1] = 1;

    Matrix B = mat_pow(A,N);
    vector<ll> dp(6,0);
    dp[0] = 1;
    for (int i = 1;i < 6;i ++) {
        for (int j = 0;j < i;j ++) {
            dp[i] += one_sixth * dp[j];
            dp[i] %= MOD;
        }
    }
    ll ans = 0;
    rep(i,6) {
        ans += B[5][i] * dp[5 - i];
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}
0