結果

問題 No.66 輝け☆全国たこやき杯
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-05 14:09:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 1,901 bytes
コンパイル時間 472 ms
コンパイル使用メモリ 62,556 KB
実行使用メモリ 69,320 KB
最終ジャッジ日時 2024-04-20 21:59:47
合計ジャッジ時間 1,449 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
69,320 KB
testcase_01 AC 47 ms
68,992 KB
testcase_02 AC 51 ms
69,264 KB
testcase_03 AC 45 ms
69,200 KB
testcase_04 AC 44 ms
68,992 KB
testcase_05 AC 43 ms
68,996 KB
testcase_06 AC 44 ms
69,096 KB
testcase_07 AC 43 ms
69,248 KB
testcase_08 AC 44 ms
69,212 KB
testcase_09 AC 44 ms
69,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
using namespace std;

const int kMAX_M = 11;

int M;
int S[1 << kMAX_M];
double memo[(1 << kMAX_M) * 2][1 << kMAX_M]; // memo[node][player]

int Pow2(int n) {
    return n * n;
}

double WinningProbability(int node_id, int player, int depth) {
    if (memo[node_id][player] >= 0.0) return memo[node_id][player];
    if (depth == M) {
        return memo[node_id][player] = 1.0;
    }

    int player_num = 1 << (M - depth), my_lb, my_ub;
    // (1 << depth) - 1 => 一番左のいとこのid
    my_lb = (node_id - ((1 << depth) - 1)) * player_num;
    my_ub = my_lb + player_num;

    int l_id = 2 * node_id + 1, r_id = 2 * node_id + 2;
    double res = 0.0;
    if (player < (my_lb + my_ub) / 2) {  // プレイヤーが左の子に存在する場合
        for (int enemy = (my_lb + my_ub) / 2; enemy < my_ub; enemy++) {
            res += (double)Pow2(S[player]) / (double)(Pow2(S[player]) + Pow2(S[enemy])) *
                        WinningProbability(l_id, player, depth + 1) * WinningProbability(r_id, enemy, depth + 1);
        }
    } else {    // プレイヤーが右の子に存在する場合
        for (int enemy = my_lb; enemy < (my_lb + my_ub) / 2; enemy++) {
            res += (double)Pow2(S[player]) / (double)(Pow2(S[player]) + Pow2(S[enemy])) *
                        WinningProbability(l_id, enemy, depth + 1) * WinningProbability(r_id, player, depth + 1);
        }
    }
    return memo[node_id][player] = res;
}

void Solve() {
    for (int i = 0; i < (1 << kMAX_M) * 2; i++) {
        for (int j = 0; j < (1 << kMAX_M); j++) {
            memo[i][j] = -1.0;
        }
    }
    cout << fixed << setprecision(10) << WinningProbability(0, 0, 0) << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> M;

    for (int i = 0; i < (1 << M); i++) {
        cin >> S[i];
    }

    Solve();

    return 0;
}
0