栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Desserts(思维+组合数学)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Desserts(思维+组合数学)

Desserts

[link](Problem - G - Codeforces)

题意

给你 n n n堆糖果每堆有 a i a_i ai​个糖,人数依次从 1 1 1到 m m m,每个人每种糖果最多拿一个,让你求出人数从 1 1 1到 m m m把糖果分完分别有多少种分法。

题解

每个人拿每种糖是独立的,所以当人数为 k k k时的答案为 ∏ i = 1 n C k a i prod_{i=1}^{n} C_{k}^{a_i} ∏i=1n​Ckai​​,这样的话枚举人数再枚举糖果至少是个 O ( n 2 ) O(n^2) O(n2)的。发现题中保证 ∑ i = 1 n a i ≤ 1 × 1 0 5 sum_{i=1}^{n}a_ile1times 10^5 ∑i=1n​ai​≤1×105,假设每堆糖果数量都不同也就是 0 + 1 + 2 + 3 + 4 + . . . + x ≤ 1 × 1 0 5 0+ 1+2+3+4+...+x le1times 10^5 0+1+2+3+4+...+x≤1×105,通过上式发现糖果最多有根号种,直接枚举每种不同的 a i a_i ai​,相同的 a i a_i ai​快速幂贡献在一起即可。

Code
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  
#include 
#include 
#include  
#include 
#define x first
#define y second
#define debug(x) cout<<#x<<":"< PII;
typedef pair PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 998244353;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N];
LL fact[N], infact[N];
bool ok[N];
int cnt[N];
vector pa;
LL qmi(LL a, LL b) {
    LL res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
LL C(int n, int m) {
    if (m > n) return 0;
    return fact[n] * infact[m] % mod * infact[n - m] % mod;
}
void init() {
    fact[0] = infact[0] = infact[1] = 1;
    for (int i = 1; i < N; i ++ ) fact[i] = fact[i - 1] * i % mod;
    for (int i = 2; i < N; i ++ ) infact[i] = (LL)(mod - mod / i) * infact[mod % i] % mod;
    for (int i = 2; i < N; i ++ ) infact[i] = infact[i] * infact[i - 1] % mod;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> m;
    init();
    for (int i = 1; i <= n; i ++) {
        int x; cin >> x;
        if (!ok[x]) pa.push_back(x), ok[x] = true; 
        cnt[x] ++;        
    }
    LL res;
    for (int i = 1; i <= m; i ++ ) {
        res = 1;
        for (auto x : pa) res = res * qmi(C(i,  x), cnt[x]) % mod;
        cout << res << endl;
    }
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589454.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号