---
id: 774
lang: en
translation_of: index.md
source_sha256: 4f9c451d05b5c8bc0a4a8cb41d41fa5243a9af9d62c992cb1cce43518aab253a
title: "Educational DP Contest: J - Sushi — Solution"
description: "With at most 3 pieces per plate, the state is the number of plates holding each count, and the expectation comes from a memoized search."
tags: ["OI","DP","Expected Value","Probability and Expectation"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea

Ahh, I'm so <span class="slang" data-zh="菜" data-gloss="cài, “vegetable”. Slang for being bad at something; weak.">weak</span>.

Since each plate holds at most 3 pieces of sushi, we can use three parameters, each standing for the number of plates with $i$ pieces of sushi. Then we can get this equation:

$$
tot = A + B + C, dp(A,B,C) = \frac{n}{tot} + \frac{A}{tot}*dp(A-1,B,C) + \frac{B}{tot}*dp(A+1,B-1,C) + \frac{C}{tot}*dp(A,B+1,C-1)
$$

Since there is no fixed order in which to do the DP, we just do a memoized search.

## Code

```cpp
// J.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 330;
int n, ai[MAX_N], sushi[4];
double dp[MAX_N][MAX_N][MAX_N];
double dfs(int a, int b, int c)
{
    if (a == 0 && b == 0 && c == 0)
        return 0;
    if (dp[a][b][c] >= 0)
        return dp[a][b][c];
    double d = a + b + c, ans = n / d;
    if (a)
        ans += dfs(a - 1, b, c) * a / d;
    if (b)
        ans += dfs(a + 1, b - 1, c) * b / d;
    if (c)
        ans += dfs(a, b + 1, c - 1) * c / d;
    return dp[a][b][c] = ans;
}
int main()
{
    memset(dp, -1, sizeof(dp));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]), sushi[ai[i]]++;
    printf("%.10lf", dfs(sushi[1], sushi[2], sushi[3]));
    return 0;
}
```
