結果

問題 No.1073 無限すごろく
ユーザー legosukelegosuke
提出日時 2020-06-16 05:34:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,573 bytes
コンパイル時間 2,008 ms
コンパイル使用メモリ 200,360 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-16 10:50:22
合計ジャッジ時間 4,089 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

template <long long MOD> struct Fp {
    long long v;
    Fp(long long v = 0) : v(v % MOD) { if (v < 0) v += MOD; }
    long long get_mod() const { return MOD; }
    long long value() const { return v; }
    Fp operator - () const { return v ? MOD - v : 0; }
    Fp operator + (const Fp &r) const { return Fp(*this) += r; }
    Fp operator - (const Fp &r) const { return Fp(*this) -= r; }
    Fp operator * (const Fp &r) const { return Fp(*this) *= r; }
    Fp operator / (const Fp &r) const { return Fp(*this) /= r; }
    Fp& operator += (const Fp &r) { v += r.v; if (v >= MOD) v -= MOD; return *this; }
    Fp& operator -= (const Fp &r) { v -= r.v; if (v < 0) v += MOD; return *this; }
    Fp& operator *= (const Fp &r) { v = v * r.v % MOD; return *this; }
    Fp& operator /= (const Fp &r) { v = v * r.inv() % MOD; return *this; }
    bool operator == (const Fp &r) const { return this->v == r.v; }
    bool operator != (const Fp &r) const { return this->v != r.v; }
    friend ostream &operator << (ostream &os, const Fp<MOD> &x) { return os << x.v; }
    friend istream &operator >> (istream &is, Fp<MOD> &x) { return is >> x.v; }
    Fp<MOD> pow(long long n);
    Fp<MOD> inv();
};
using mint = Fp<1000000007>;

template <long long MOD> Fp<MOD> Fp<MOD>::pow(long long n) {
    Fp<MOD> res(1), tmp(v);
    while (n > 0) {
        if (n & 1) res *= tmp;
        tmp *= tmp;
        n >>= 1;
    }
    return res;
}

template <long long MOD> Fp<MOD> Fp<MOD>::inv() {
    long long a = v, b = MOD, x = 1, y = 0, t;
    while (b > 0) {
        t = a / b;
        swap(a -= t * b, b);
        swap(x -= t * y, y);
    }
    x %= MOD;
    if (x < 0) x += MOD;
    return x;
}

template <class T, size_t N> struct Mat {
    array<array<T, N>, N> A;
    Mat(T x = T(0)) { for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) A[i][j] = x; }
    inline const array<T, N>& operator [] (size_t i) const { return A[i]; }
    inline array<T, N>& operator [] (size_t i) { return A[i]; }
    Mat operator * (const Mat &B) const;
    Mat operator + (const Mat &B) const;
    Mat operator - (const Mat &B) const;
    Mat pow(long long n) const;
};

template <class T, size_t N> Mat<T, N> Mat<T, N>::operator * (const Mat &B) const {
    Mat C;
    for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) for (int k = 0; k < N; ++k) {
        C[i][j] = C[i][j] + (A[i][k] * B[k][j]);
    }
    return C;
}

template <class T, size_t N> Mat<T, N> Mat<T, N>::operator + (const Mat &B) const {
    Mat C;
    for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) {
        C[i][j] = A[i][j] + B[i][j];
    }
    return C;
}

template <class T, size_t N> Mat<T, N> Mat<T, N>::operator - (const Mat &B) const {
    Mat C;
    for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) {
        C[i][j] = A[i][j] - B[i][j];
    }
    return C;
}

template <class T, size_t N> Mat<T, N> Mat<T, N>::pow(long long n) const {
    Mat B = *this, res;
    for (int i = 0; i < N; ++i) res[i][i] = 1;
    while (n) {
        if (n & 1) res = res * B;
        B = B * B;
        n >>= 1;
    }
    return res;
}

void _main() {
    int N;
    cin >> N;
    Mat<mint, 6> A;
    REP (i, 6) A[0][i] = mint(6).inv();
    REP (i, 5) A[i + 1][i] = mint(1);
    cout << A.pow(N)[0][0] << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    _main();
    return 0;
}
0