結果

問題 No.75 回数の期待値の問題
ユーザー PachicobuePachicobue
提出日時 2017-07-22 14:47:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 480 ms / 5,000 ms
コード長 1,916 bytes
コンパイル時間 1,742 ms
コンパイル使用メモリ 175,616 KB
実行使用メモリ 4,868 KB
最終ジャッジ日時 2023-07-30 14:26:26
合計ジャッジ時間 4,493 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 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,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 40 ms
4,380 KB
testcase_17 AC 219 ms
4,380 KB
testcase_18 AC 382 ms
4,672 KB
testcase_19 AC 480 ms
4,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

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

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;


struct Matrix {
    Matrix(int n) : size(n)
    {
        table.resize(n, vector<long double>(n, 0));
    }
    Matrix operator*(const Matrix& mat) const
    {
        assert(mat.size == size);
        Matrix result(size);
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                for (int k = 0; k < size; k++) {
                    result.table[i][j] += table[i][k] * mat.table[k][j];
                }
            }
        }
        return result;
    }
    const int size;
    vector<vector<long double>> table;
};

Matrix power(const Matrix& m, int n)
{
    if (n == 1) {
        return m;
    }
    if (n % 2 == 1) {
        return m * power(m, n - 1);
    } else {
        const Matrix p = power(m, n / 2);
        return p * p;
    }
}

int main()
{
    int N;
    cin >> N;
    Matrix m(N + 1);
    for (int i = 0; i <= N; i++) {
        for (int j = 1; j <= 6; j++) {
            int n = i + j;
            if (n > N) {
                n = 0;
            }
            m.table[i][n] += (long double)1.0 / 6;
        }
    }

    constexpr int rep = 10000;
    const Matrix result = power(m, rep);
    //    show(result.table);
    cout << (long double)1.0 / result.table[0][N] - 1.0 << endl;
    return 0;
}
0