結果

問題 No.75 回数の期待値の問題
ユーザー codershifthcodershifth
提出日時 2015-08-02 23:02:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,002 bytes
コンパイル時間 1,311 ms
コンパイル使用メモリ 145,588 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 00:35:26
合計ジャッジ時間 2,363 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class CountExpectationProblem {
public:
    // 残りの数値 k から終了までのサイコロをふる回数の期待値を Exp(k) とする。
    // このとき
    //
    //  * Exp(0) = 0
    //  * Exp(k) = Exp(K) (k < 0) (K を超えたら 0 にリセットされるので)
    //  * Exp(k) = 1 + (Exp(k-1)+...+Exp(k-6))/6    (k>0)
    //
    // この連立方程式から Exp(K) を計算すれば良い
    //

    // Exp(K) = x として漸化式を計算する。
    double F(int K, double x) {
            vector<double> Exp(K+1,0);
            FOR(k,1,K+1)
            {
                Exp[k] = 1.0;
                for (int i = 1; i <= 6; ++i)
                    Exp[k] += ((k-i>=0)? Exp[k-i] : x)/6.0; // k<0 のときは x (=~Exp(K)) を使う
            }
            return Exp[K];
    }
    void solve_binserach(void) {
            int K;
            cin>>K;

            //
            // 漸化式より Exp(k) は初項 x = Exp(K) の関数 F(k,x) = Exp(k) とみなせる。
            // y = F(x) = F(K,x) とすると F は明らかに x の単調増加関数
            // よって二分探索で y=x との交点を計算できる。
            double low  = 0.0;
            double high = 10000;
            REP(i,100)
            {
                double mid = (low+high)*0.5;
                if (F(K, mid) > mid)
                    low = mid;
                else
                    high = mid;
            }
            cout<<setprecision(20)<<(low+high)*0.5<<endl;
    }
    void solve(void) {
            solve_binserach();
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new CountExpectationProblem();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0