BZOJ4665: Little w's Wedding Candy — Solution
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)!} \]
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
// 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;
}