当前位置:网站首页>CCPC 2021威海 - G. Shinyruo and KFC(组合数,小技巧)
CCPC 2021威海 - G. Shinyruo and KFC(组合数,小技巧)
2022-07-05 20:08:00 【小酒窝.】
G. Shinyruo and KFC
题意
一共有 n n n 种食物,每种食物有 a i a_i ai 个。食物个数总和不超过 1 0 5 10^5 105。
现在要把这些食物分给 k k k 个人,每个人可以拿多种事物,但每种食物最多拿 1 个。
对于 k k k 从 1 到 m m m,分别输出食物分配方案。
答案模 998244353 998244353 998244353。
1 ≤ m , n ≤ 5 ⋅ 1 0 4 1 \le m,n \le 5 \cdot 10^4 1≤m,n≤5⋅104
0 ≤ a i ≤ 1 0 5 , ∑ i = 1 n a i ≤ 1 0 5 0\le a_i\le 10^5,\ \sum_{i=1}^n a_i\le 10^5 0≤ai≤105, ∑i=1nai≤105
思路
对于一种食物共有 a i a_i ai 个,分给 k k k 个人,每人最多分一个,那么分配方案为 C ( k , a i ) C(k, ai) C(k,ai)。
那么 n 种食物,分给 k 个人总的分配方案为: ∏ i = 1 n C ( k , a i ) \prod_{i=1}^{n} C(k, a_i) ∏i=1nC(k,ai);
再遍历 m 个人,这样的话时间复杂度最低为 O(n * m),超时。
而题目中说 ai 总和不超过 1e5,那么 ai 的种类数就在 1 e 5 \sqrt {1e5} 1e5 级别,对于重复的直接用快速幂求幂次。
预处理逆元求组合数。
这样时间复杂度降为: O ( m n l o g n ) O(m \sqrt n \ logn) O(mn logn)
Code
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
#define int long long
#define fi first
#define se second
#define endl '\n'
map<int,int> mp;
const int N = 200010, mod = 998244353;
int T, n, m;
int a[N];
struct node{
int x, cnt;
}b[N];
int fac[N], inv[N], finv[N];
int qmi(int x, int y)
{
int ans = 1;
while(y)
{
if(y & 1) ans = ans * x % mod;
x = x*x % mod;
y >>= 1;
}
return ans;
}
void init(){
fac[0]=inv[0]=inv[1]=finv[0]=finv[1]=1ll;
for(int i=1;i<N;i++)fac[i]=fac[i-1]*i%mod;
for(int i=2;i<N;i++)inv[i]=(mod-mod/i)*inv[mod%i]%mod;
for(int i=2;i<N;i++)finv[i]=finv[i-1]*inv[i]%mod;
}
inline int C(int a,int b){
if(b>a)return 0;
else return fac[a]*finv[a-b]%mod*finv[b]%mod;
}
signed main(){
Ios;
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> a[i], mp[a[i]]++;
int cnt = 0;
for(auto it : mp)
{
cnt++;
b[cnt] = {
it.first, it.second};
}
init();
for(int i=1;i<=m;i++)
{
int ans = 1;
for(int j=1;j<=cnt;j++)
{
ans = ans * qmi(C(i, b[j].x), b[j].cnt) % mod;
}
cout << ans << endl;
}
return 0;
}
经验
- 如果 n 个数的总和不超过 m,那么这些数的种类个数在 m \sqrt m m 级别。
边栏推荐
- Bzoj 3747 poi2015 kinoman segment tree
- Go language | 03 array, pointer, slice usage
- Interviewer: what is the internal implementation of set data types in redis?
- C - sequential structure
- 计算lnx的一种方式
- leetcode刷题:二叉树14(左叶子之和)
- How to retrieve the root password of MySQL if you forget it
- leetcode刷题:二叉树10(完全二叉树的节点个数)
- 线程池参数及合理设置
- 1:引文;
猜你喜欢

leetcode刷题:二叉树16(路径总和)

2023年深圳市绿色低碳产业扶持计划申报指南

计算lnx的一种方式

.Net分布式事务及落地解决方案

实操演示:产研团队如何高效构建需求工作流?

Elk distributed log analysis system deployment (Huawei cloud)
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises

Force buckle 1200 Minimum absolute difference

秋招字节面试官问你还有什么问题?其实你已经踩雷了

Go language | 03 array, pointer, slice usage
随机推荐
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
Relationship between floating elements and parent and brother boxes
安信证券在网上开户安全吗?
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
Flume series: interceptor filtering data
How to safely and quickly migrate from CentOS to openeuler
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
Ffplay document [easy to understand]
Multi branch structure
c语言oj得pe,ACM入门之OJ~
Leetcode brush questions: binary tree 18 (largest binary tree)
Leetcode skimming: binary tree 12 (all paths of binary tree)
A solution to PHP's inability to convert strings into JSON
银河证券在网上开户安全吗?
DP: tree DP
解决php无法将string转换为json的办法
期货如何网上开户?安不安全?
Elk distributed log analysis system deployment (Huawei cloud)
计算lnx的一种方式
函数的概念及语法