[ABC235G] Gardens
Problem Statement
Takahashi has $A$ apple seedlings, $B$ banana seedlings, and $C$ cherry seedlings. Seedlings of the same kind cannot be distinguished.
He will plant these seedlings in his $N$ gardens so that all of the following conditions are satisfied.
- At least one seedling must be planted in every garden.
- It is not allowed to plant two or more seedlings of the same kind in the same garden.
- It is not necessary to plant all seedlings he has.
How many ways are there to plant seedlings to satisfy the conditions? Find the count modulo $998244353$.
Two ways are distinguished when there is a garden with different sets of seedlings planted in these two ways.
Constraints
- $1 \leq N \leq 5 \times 10^6$
- $0 \leq A \leq N$
- $0 \leq B \leq N$
- $0 \leq C \leq N$
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $A$ $B$ $C$
Output
Print the answer.
Sample Input 1
2 2 1 1
Sample Output 1
21
As illustrated below, there are $21$ ways to plant seedlings to satisfy the conditions.
(The two frames arranged vertically are the gardens. $A, B, C$ stand for apple, banana, cherry, respectively.)
如果不考虑每个花园中都要种种子的限制,怎么做?
枚举总共会泼洒几个苹果的种子,几个香蕉的种子,几个樱桃的种子,然后用组合数进行计算。用表达式来写就是 \((\sum\limits_{i=0}^aC_n^i)(\sum\limits_{i=0}^bC_n^i)(\sum\limits_{i=0}^cC_n^i)\)
为了方便,设 \(S(n,m)=\sum\limits_{i=0}^mC_n^i\),上面的表达式可以写作 \(S(n,a)\times S(n,b)\times S(n,c)\)
现在要加上每个花园中都要种种子的条件,明显用容斥原理。
定义 \(f(i)\) 为至多有 \(i\) 个花园中有种子的情况数,那么最终答案为 \(f(n)-f(n-1)+f(n-2)-\cdots\)
我们需要快速算出 \(S(n,m)\),但这不太可能有很好的通项公式。如果 \(n\) 为定值,那就处理一个前缀和就可以了,但现在 \(n\) 不为定值, \(m\) 才是。考虑杨辉三角的转移公式,我们可以发现从 \(S(n,m)\) 到 \(S(n+1,m)\) 的路程中,除了 \(C_n^m\),全部算了两次。可以得到转移式子 \(S(n,m)=S(n-1,m)+C_{n-1}^m\)。所以计算 \(m=a,b,c\) 时的答案,套容斥式子就可以了。
#include<cstdio>
const int P=998244353,N=5e6+5;;
int n,a,b,c,f[N],fa[N],fb[N],fc[N],inv[N],iv[N],ans;
int calc(int x,int y)
{
if(x<y)
return 0;
return 1LL*f[x]*iv[y]%P*iv[x-y]%P;
}
int main()
{
scanf("%d%d%d%d",&n,&a,&b,&c);
iv[0]=iv[1]=inv[1]=f[0]=f[1]=1;
for(int i=2;i<=n;i++)
{
inv[i]=1LL*(P-P/i)*inv[P%i]%P;
iv[i]=iv[i-1]*1LL*inv[i]%P;
f[i]=f[i-1]*1LL*i%P;
}
fa[0]=fb[0]=fc[0]=1;
for(int i=1;i<=n;i++)
{
fa[i]=(fa[i-1]*2LL-calc(i-1,a)+P)%P;
fb[i]=(fb[i-1]*2LL-calc(i-1,b)+P)%P;
fc[i]=(fc[i-1]*2LL-calc(i-1,c)+P)%P;
// printf("%d %d %d\n",fa[i],fb[i],fc[i]);
}
for(int i=0;i<=n;i++)
(ans+=(i&1? -1LL:1LL)*calc(n,i)*fa[n-i]%P*fb[n-i]%P*fc[n-i]%P)%=P;
printf("%d",(ans+P)%P);
}
[ABC235G] Gardens的更多相关文章
- Vue.js——使用$.ajax和vue-resource实现OAuth的注册、登录、注销和API调用
概述 上一篇我们介绍了如何使用vue resource处理HTTP请求,结合服务端的REST API,就能够很容易地构建一个增删查改应用.这个应用始终遗留了一个问题,Web App在访问REST AP ...
- Vue.js——基于$.ajax实现数据的跨域增删查改
概述 之前我们学习了Vue.js的一些基础知识,以及如何开发一个组件,然而那些示例的数据都是local的.在实际的应用中,几乎90%的数据是来源于服务端的,前端和服务端之间的数据交互一般是通过ajax ...
- Lesson 8 The best and the worst
Text Joe Sanders has the most beautiful garden in our town. Nearly everbody enters for 'The Nicest G ...
- Lesson 3 Please send me a card
Text Postcards always spoil my holidays. Last summer, I went to Italy. I visited museums and sat in ...
- iis
IIS架构 1. 概述 为了提高IIS的可靠性,安全性以及可用性,与IIS5.0和以前更早的版本不同,IIS6.0提供了一个全新的IIS架构.这个架构的详细情况如下图所示: ...
- Google Maps API V3 之 路线服务
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- Python开发【前端】:jQuery
jQuery简介 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是&qu ...
- hihoCoder 1425 : What a Beautiful Lake(美丽滴湖)
hihoCoder #1425 : What a Beautiful Lake(美丽滴湖) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 ...
- Quant的笑话
Q) Why was the FX quant so unlucky with the ladies?A) Because he always kept his dates short. Q) Why ...
- XQuery的 value() 方法、 exist() 方法 和 nodes() 方法
Xml数据类型 /*------------------------------------------------------------------------------+ #| = : = : ...
随机推荐
- 聊聊基于Alink库的推荐系统
概述 Alink提供了一系列与推荐相关的组件,从组件使用得角度来看,需要重点关注如下三个方面: 算法选择 推荐领域有很多算法,常用的有基于物品/用户的协同过滤.ALS.FM算法等.对于不同的数据场景, ...
- 不能显式拦截ajax请求的302响应?
记录工作中早该加深印象的一个小case: ajax请求不能显式拦截 302响应. 我们先来看一个常规的登录case: 浏览器请求资源,服务器发现该请求未携带相关凭据(cookie或者token) 服务 ...
- Insert a scratch project into a ppt (MSPowerPoinT file)在powerpoint中播放Scratch动画
Insert a scratch project into a ppt (MSPowerPoinT file)在powerpoint中播放Scratch动画 Contributed by liu pe ...
- 单元测验3:亲密关系mooc
单元测验3:亲密关系 查看帮助 返回 1 单选(2分) 在亲密关系中,有关权力的表述,以下说法不太准确的的是? A. 对关系付出越多,权力越大. B. 大部分人会倾向认为,在恋爱关系中,男女应该拥 ...
- html表单与框架
1.以form开头 其中常用的属性有 action="" method="" enctype="" name="" ...
- Java虚拟机(JVM):第一幕:起源,不一定全,但是一定靠谱
在学习JVM之前,先分享一则信息:2009 年4月20日,Orace 宣布正式以74 亿美元的价格收购市值曾超过2000 亿美元的Sun公司,传奇的Sun Microsystems 从此落幕成为历史. ...
- 自学一周python做的一个小游戏《大球吃小球》
需求 1,显示一个窗口. 2,我们要做到的功能有鼠标点击屏幕生成小球. 3,生成的小球大小随机,颜色随机,向随机方向移动,速度也随机. 4,大的球碰到小球时可以吃掉小球,吃掉后会变大. 5,球碰到边界 ...
- Vue 3 中用组合式函数和 Shared Worker 实现后台分片上传(带哈希计算)
01. 背景 最近项目需求里有个文件上传功能,而客户需求里的文件基本上是比较大的,基本上得有 1 GiB 以上的大小,而上传大文件尤其是读大文件,可能会造成卡 UI 或者说点不动的问题.而用后台的 W ...
- 模拟退火算法(SA)
求某个目标函数的最值 爬山法 首先我们通过爬山法来引出模拟退火算法 我们先看一个例子:求函数的最值 我们用爬山法解决这个问题的步骤 1.在解空间中随机生成一个初始解(图中小黄点就是我们生成的初始解) ...
- flask 三方模块
flask 三方插件 Flask-AppBuilder - Simple and rapid Application builder, includes detailed security, auto ...