当前位置: 首页 > article >正文

Codeforces Round 258 (Div. 2) E. Devu and Flowers 生成函数

题目链接

题目大意

n n n ( 1 ≤ n ≤ 20 ) (1\leq n \leq 20) (1n20) 个花瓶,第 i i i 个花瓶里有 f i f_i fi ( 1 ≤ f i ≤ 1 0 12 ) (1\leq f_i \leq 10^{12}) (1fi1012) 朵花。现在要选择 s s s ( 1 ≤ s ≤ 1 0 14 ) (1\leq s \leq 10^{14}) (1s1014)朵花。

求出有多少种方案。两种方案不同当且仅当两种方案中至少有一个花瓶选择花的数量不同,答案对 1 0 9 + 7 10^9+7 109+7 取模。

思路

取第 i i i 种花的生成函数可以表示为 : f i ( x ) = f_i(x)= fi(x)= 1 + x + x 2 + x 3 + ⋅ ⋅ ⋅ x f i 1+x+x^2+x^3+ \cdot \cdot \cdot x^{f_i} 1+x+x2+x3+xfi.

则选取方案的生成函数可以表示为 : G ( x ) = G(x)= G(x)= f 1 ( x ) ⋅ f 2 ( x ) ⋅ ⋅ ⋅ f n ( x ) = f_1(x) \cdot f_2(x) \cdot \cdot \cdot f_n(x) = f1(x)f2(x)fn(x)= ( 1 − x f 1 + 1 ) ( 1 − x f 2 + 1 ) ( 1 − x f 3 + 1 ) ⋅ ⋅ ⋅ ( 1 − x f n + 1 ) ( 1 − x ) n \frac {(1-x^{f_1+1})(1-x^{f_2+1})(1-x^{f_3+1}) \cdot \cdot \cdot (1-x^{f_n+1})} {(1-x)^n} (1x)n(1xf1+1)(1xf2+1)(1xf3+1)⋅⋅⋅(1xfn+1) = = = ∏ i = 1 n ( 1 − x f i + 1 ) ( 1 − x ) n \frac {\prod_{i=1}^{n}(1-x^{f_i+1})}{(1-x)^{n}} (1x)ni=1n(1xfi+1) = = = ∏ i = 1 n ( 1 − x f i + 1 ) \prod_{i=1}^{n}(1-x^{f_i+1}) i=1n(1xfi+1) ⋅ \cdot ∑ i = 0 ∞ \sum_{i=0}^\infty i=0 ( i + n − 1 i ) {i+n-1\choose i} (ii+n1) x i x_i xi = = = ∏ i = 1 n ( 1 − x f i + 1 ) \prod_{i=1}^{n}(1-x^{f_i+1}) i=1n(1xfi+1) ⋅ \cdot ∑ i = 0 ∞ \sum_{i=0}^\infty i=0 ( i + n − 1 n − 1 ) {i+n-1\choose n-1} (n1i+n1) x i x_i xi .

[ x s ] G ( x ) [x^s]G(x) [xs]G(x)即为所求的答案。

A ( x ) = ∏ i = 1 n ( 1 − x f i + 1 ) A(x)=\prod_{i=1}^{n}(1-x^{f_i+1}) A(x)=i=1n(1xfi+1), B ( x ) = ∑ i = 0 ∞ B(x)=\sum_{i=0}^\infty B(x)=i=0 ( i + n − 1 n − 1 ) x i {i+n-1\choose n-1} x_i (n1i+n1)xi.

由于 n ≤ 20 n\leq20 n20 , 所以 A ( x ) A(x) A(x) 最多只有 2 20 2^{20} 220 项,可以直接枚举,即 a n s = [ x s ] G ( x ) = ∑ i = 0 2 n [ x i ] A ( x ) ⋅ [ x s − i ] B ( x ) = ans=[x^s]G(x)=\sum_{i=0}^{2^n}[x^i]A(x) \cdot [x^{s-i}]B(x)= ans=[xs]G(x)=i=02n[xi]A(x)[xsi]B(x)= ∑ i = 0 2 n [ x i ] A ( x ) ⋅ ( s − i + n − 1 n − 1 ) \sum_{i=0}^{2^n}[x^i]A(x) \cdot {s-i+n-1\choose n-1} i=02n[xi]A(x)(n1si+n1),由于 n n n 很小可以暴力计算组合数,总的时间复杂度为 O ( n ⋅ 2 n ) O(n \cdot 2^n) O(n2n).

code

#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pii pair<int, int>

using namespace std;
const int mod = 1e9 + 7;
int f[30];

int ksm(int x, int k)
{
    int res = 1;
    while (k > 0)
    {
        if (k & 1)
            res = res * x % mod;
        x = x * x % mod;
        k >>= 1;
    }
    return res;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int n, s;
    cin >> n >> s;
    int fac = 1;
    for (int i = 1; i <= n; ++i)
    {
        cin >> f[i - 1];
        if (i < n)
            fac = fac * i % mod;
    }
    int infac = ksm(fac, mod - 2);
    int ans = 0;
    for (int i = 0; i < (1ll << n); ++i)
    {
        int cnt = 0, sum = 0;
        for (int j = 0; j < n; ++j)
        {
            if ((1ll << j) & i)
                cnt++, sum += f[j] + 1;
        }
        if (sum > s)
            continue;
        int tmp = 1;
        for (int j = 0; j < n - 1; ++j)
        {
            int p = (s - sum + n - 1 - j) % mod;
            tmp = tmp * p % mod;
        }
        tmp = tmp * infac % mod;
        ans = (ans + ((cnt & 1) ? mod - tmp : tmp) % mod) % mod;
    }
    cout << (ans + mod) % mod << '\n';
    return 0;
}

http://www.kler.cn/a/578314.html

相关文章:

  • Phi-4-multimodal:图、文、音频统一的多模态大模型架构、训练方法、数据细节
  • 智源开源多模态向量模型BGE-VL:多模态检索新突破
  • google s2部分浅讲
  • JVM - 3.垃圾回收
  • 物联网(Internet of Things, IoT)中的网络层简介
  • Eclipse 查看 JAVA SE 23 官方API 源代码
  • 为什么 MySQL InnoDB 的 Repeatable Read 可以阻止幻读?
  • 【JavaEE进阶】Spring AOP详解
  • preloaded-classes裁剪
  • 工业单板电脑在电商物流中心的应用案例
  • Java多线程与高并发专题——什么是阻塞队列?
  • Windows CMD 命令大全(综合开发整理版)
  • 【编译器】VSCODE搭建ESP32
  • C++:dp3则
  • 数据库---1.0
  • 基于SpringBoot的餐厅点餐管理系统设计与实现(源码+SQL脚本+LW+部署讲解等)
  • 【redis】全局命令set、get、keys
  • typora高亮方案+鼠标侧键一键改色
  • 解决https页面请求http出现Mixed Content.This request has been blocked; the content must be served over HTTPS.
  • vscode - 操作整理