---
id: 1221
lang: en
translation_of: index.md
source_sha256: 91174e8294fe605768a5fd616acf239529856d6a9740e27ea29e91252d247f6f
title: "P3239: [HNOI2015] King Arthur — Solution"
description: "A probability and expectation DP problem."
tags: ["OI","DP","Probability and Expectation","Complement Counting"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 亚瑟王 rendered as “King Arthur”."
  - "每一个点的贡献 kept literally as “each point's contribution” (the points are the cards)."
  - "威力 rendered as “power” (the card's damage)."
---

## Solution

A probability and expectation DP problem.

Our state won't take the current round into account: that would be too hard to design, and I'm too <span class="slang" data-zh="菜" data-gloss="cài, “vegetable”. Slang for being bad at something; weak.">weak</span> to manage it. Let's design a global state: $f[i][j]$ is the probability that, considering the first $i$ cards, only $j$ cards have been activated by the time the game ends. Then the answer is:

<!--more-->

$$
ans = \sum_{i = 1}^n d_i \sum_{j = 0}^r dp[i-1][j] * [1 - (1 - p_i)^{r - j}]
$$

Analyzing this formula: for the expectation, we can add up each point's contribution linearly, the power times the probability that it activates successfully; the probability of success can be split into the success probabilities for $j$ cards having activated successfully, and after computing the probability of failing every time in a row, we just take the complement.

Now let's think about how to compute $dp$. First notice that these cards are already in a fixed order:

$$
dp[i][j] = dp[i-1][j] * (1-p_i)^{r - j} + dp[i-1][j-1] * (1 - (1 - p_i)^{r - (j - 1)})
$$

Let's unpack this formula:

- Transition from $dp[i-1][j]$: this card is not activated if and only if it does not activate in the remaining $r-j$ rounds, so we write the power of the complement probability.
- Transition from $dp[i-1][j-1]$: this card is activated (**in order**) if and only if it has a chance to activate in the remaining $r-(j-1)$ rounds, so it's the complement again.

## Code

```cpp
// P3239.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 300;
int T, n, r, di[MAX_N];
double pi[MAX_N], dp[MAX_N][MAX_N], f[MAX_N];

double quick_power(double bas, int tim)
{
    double ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas;
        bas = bas * bas;
        tim >>= 1;
    }
    return ans;
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        memset(dp, 0, sizeof(dp)), memset(f, 0, sizeof(f));
        scanf("%d%d", &n, &r);
        for (int i = 1; i <= n; i++)
            scanf("%lf%d", &pi[i], &di[i]);
        dp[1][0] = quick_power(1 - pi[1], r);
        dp[1][1] = f[1] = 1 - dp[1][0];
        for (int i = 2; i <= n; i++)
        {
            f[i] = 0;
            for (int j = 0; j <= r; j++)
            {
                dp[i][j] = dp[i - 1][j] * quick_power(1 - pi[i], r - j);
                if (j > 0)
                    dp[i][j] += dp[i - 1][j - 1] * (1 - quick_power(1 - pi[i], r - j + 1));
                f[i] += dp[i - 1][j] * (1 - quick_power(1 - pi[i], r - j));
            }
        }
        double ans = 0;
        for (int i = 1; i <= n; i++)
            ans += f[i] * di[i];
        printf("%.10lf\n", ans);
    }
    return 0;
}
```
