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=1nCkai,这样的话枚举人数再枚举糖果至少是个 O ( n 2 ) O(n^2) O(n2)的。发现题中保证 ∑ i = 1 n a i ≤ 1 × 1 0 5 \sum_{i=1}^{n}a_i\le1\times 10^5 ∑i=1nai≤1×105,假设每堆糖果数量都不同也就是 0 + 1 + 2 + 3 + 4 + . . . + x ≤ 1 × 1 0 5 0+ 1+2+3+4+...+x \le1\times 10^5 0+1+2+3+4+...+x≤1×105,通过上式发现糖果最多有根号种,直接枚举每种不同的 a i a_i ai,相同的 a i a_i ai快速幂贡献在一起即可。
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> 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<int> 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;
}
这篇博客介绍了如何解决一个关于糖果分配的问题。给定每堆糖果的数量,求解在不同人数下分配所有糖果的方法数。博主利用组合数学的原理,结合快速幂优化算法,将时间复杂度降低,解决了这个问题。文章详细阐述了算法思想和实现过程,并给出了代码实现。

被折叠的 条评论
为什么被折叠?



