---
id: 1190
lang: en
translation_of: index.md
source_sha256: d7dabe3f37f6474869ebb24accbfadb7f222e890a7ff2bed414b63a856c66fd8
title: "BZOJ4665: Little w's Wedding Candy — Solution"
description: "A DP counts the ways for exactly j candies to keep their original color, then inclusion–exclusion with factorials gives the answer."
tags: ["OI","Math","DP","Inclusion–Exclusion"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "注意这里颜色不一样不是本质不同的 rendered literally as “note that here, a different color is not what makes things essentially distinct”; read with the later remark that all candies are treated as essentially distinct."
  - "前i个状态 kept literally as “the first i states”, although it presumably means the first i colors."
---

## Solution

This problem is DP + inclusion–exclusion, which happens to be exactly the type I'm not very good at. Let the state $f[i][j]$ be “the number of ways in which, after considering the first $i$ states, $j$ colors are the same as the original (note that here, a different color is not what makes things essentially distinct)”. We can derive the formula:

$$
dp[i][j] = \sum_{k = 0}^j dp[i - 1][j - k] {cnt[i] \choose k} \frac{cnt[i]!}{(cnt[i] - k)!}
$$

<!--more-->

Here we enumerate $k$ as the number of wedding candies in this step that have the same color as their own original one; after transitioning from the previous state we multiply by the binomial coefficient (the ways to choose), and then multiply by the number of ways to pick the wedding candies (for now we treat all the candies as essentially distinct things).

For the final answer, do a bit of inclusion–exclusion:

$$
ans = dp[n][0]*(n-0)! - dp[n][1]*(n-1)! + dp[n][2]*(n-2)! \dots
$$

where the factorial stands for the number of ways for the remaining people to pick candies freely.

## Code

```cpp
// BZ4665.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll MAX_N = 2020, mod = 1000000009;
ll n, arr[MAX_N], level[MAX_N], level_rev[MAX_N], dp[MAX_N][MAX_N], cnt[MAX_N];

ll quick_pow(ll bas, ll tim)
{
    ll ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas % mod;
        bas = bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}

ll comb(ll a, ll b)
{
    return level[a] * level_rev[a - b] % mod * level_rev[b] % mod;
}

int main()
{
    scanf("%lld", &n);
    for (ll i = 1; i <= n; i++)
        scanf("%lld", &arr[i]), cnt[arr[i]]++;

    level[0] = level[1] = level_rev[0] = level_rev[1] = 1;
    for (ll i = 2; i < MAX_N; i++)
        level[i] = level[i - 1] * i % mod;
    level_rev[MAX_N - 1] = quick_pow(level[MAX_N - 1], mod - 2);
    for (ll i = MAX_N - 1; i >= 3; i--)
        level_rev[i - 1] = level_rev[i] * i % mod;
    dp[0][0] = 1;
    for (ll i = 1; i <= n; i++)
        for (ll j = 0; j <= n; j++)
            for (ll k = 0; k <= cnt[i]; k++)
            {
                if (k > j)
                    break;
                dp[i][j] = (dp[i][j] + dp[i - 1][j - k] * comb(cnt[i], k) % mod * level[cnt[i]] % mod * level_rev[cnt[i] - k] % mod) % mod;
            }
    ll ans = 0;
    for (ll i = 0; i <= n; i++)
    {
        dp[n][i] = (dp[n][i] * level[n - i] % mod);
        if (i & 1)
            dp[n][i] = (dp[n][i] * (-1) + mod) % mod;
        ans = (ans + dp[n][i] + mod) % mod;
    }
    for (ll i = 1; i <= n; i++)
        ans = ans * level_rev[cnt[i]] % mod;
    printf("%lld", ans);
    return 0;
}
```
