---
id: 842
lang: en
translation_of: index.md
source_sha256: 96f322673c5447fd421d022f9023a0b6c0aa8208e8596116de928b305b23dffa
title: "Codeforces 451E: Devu and Flowers — Solution"
description: "A fairly direct multiset combinations problem: apply the formula and enumerate the subsets with binary decomposition."
tags: ["OI","Combinatorics","Combinatorics","Multisets","Inclusion–Exclusion"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "增强版组合数 rendered literally as “enhanced combination number”, keeping the author's quotation marks."
  - "二进制分解 rendered literally as “binary decomposition” (the code enumerates subsets as bitmasks)."
  - "《算法竞赛进阶指南》 rendered as the italicised title Advanced Guide to Algorithm Competitions, as in other posts; the author's own “source:” label kept as written."
  - "Tags 组合 and 组合数学 both map to Combinatorics in tags.yaml, so the tag appears twice."
---

## Main Idea

This problem requires knowledge of multisets. If you haven’t learned it, see the multiset section of [this blog post](../668-oi-combinatorics/index.en.html).

Clearly, the problem asks us to find the number of combinations of a multiset, and it’s the “enhanced combination number”. So we just follow the formula and compute it using the binary decomposition method. This is a fairly <span class="slang" data-zh="裸" data-gloss="luǒ, “naked”. OI slang for plain, standard material with no twist on top.">bare</span> multiset combinations problem.

## Code

```cpp
// CF451E.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1e9 + 7, MAX_N = 25;
ll arr[MAX_N], n, inv[MAX_N], m;
ll quick_power(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 C(ll a, ll b)
{
    if (a < 0 || b < 0 || a < b)
        return 0;
    a %= mod;
    if (a == 0 || b == 0)
        return 1;
    ll ans = 1;
    for (int i = 0; i < b; i++)
        ans = ans * (a - i) % mod;
    for (int i = 1; i <= b; i++)
        ans = ans * inv[i] % mod;
    return ans;
}
int main()
{
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &arr[i]);
    inv[0] = 1;
    for (int i = 1; i < MAX_N; i++)
        inv[i] = quick_power(i, mod - 2);
    ll ans = 0;
    for (int stat = 0; stat < (1 << n); stat++)
        if (stat == 0)
            ans = (ans + C(n + m - 1, n - 1)) % mod;
        else
        {
            ll t = n + m, p = 0;
            for (int i = 0; i < n; i++)
                if ((stat >> i) & 1)
                    p++, t -= arr[i + 1];
            t -= (p + 1);
            if (p & 1)
                ans = (ans - C(t, n - 1)) % mod;
            else
                ans = (ans + C(t, n - 1)) % mod;
        }
    ans = (ans + mod) % mod;
    printf("%lld", ans);
    return 0;
}
```

source: Page 172, *Advanced Guide to Algorithm Competitions*, by Li Yudong
