結果

問題 No.1073 無限すごろく
ユーザー QDSNQDSN
提出日時 2020-07-01 11:21:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,909 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 174,012 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-11 22:02:23
合計ジャッジ時間 4,753 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
ll k;
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a % b, y, x); // 再帰的に解く
    y -= a / b * x;
    return d;
}

// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m) { return (a % m + m) % m; }

// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
    long long x, y;
    extGCD(a, m, x, y);
    return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
vector<vector<ll>> ope(vector<vector<ll>> &mat1, vector<vector<ll>> &mat2) {
    vector<vector<ll>> res(k, vector<ll>(k, 0));
    for(int i = 0; i < k; i++) {
        for(int j = 0; j < k; j++) {
            for(int l = 0; l < k; l++) {
                res[i][j] += mat1[i][l] * mat2[l][j] % MOD;
                res[i][j] %= MOD;
            }
        }
    }
    return res;
}
vector<vector<ll>> modpow(vector<vector<ll>> a, ll n) {
    vector<vector<ll>> res = a;
    n--;
    while(n > 0) {
        if(n & 1)
            res = ope(res, a);
        a = ope(a, a);
        n >>= 1;
    }
    return res;
}
int main() {
    ll n;
    cin >> n;
    n += 5;
    k = 6;
    vector<ll> init = {1, 0, 0, 0, 0, 0};
    vector<vector<ll>> mat(k, vector<ll>(k, 0));
    for(int i = 0; i < k; i++) {
        mat[0][i] = modinv(6, MOD);
    }
    for(int i = 1; i < k; i++) {
        for(int j = 0; j < k; j++) {
            mat[i][i - 1] = 1;
        }
    }
    auto ret = modpow(mat, n - 5);
    ll ans = 0;
    for(int i = 0; i < k; i++) {
        ans += ret[0][i] * init[i] % MOD;
        ans %= MOD;
    }
    cout << ans << endl;
}
0