[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数据类型 /*------------------------------------------------------------------------------+ #| = : = : ...
随机推荐
- 6、Mybatis之高级查询
6.1.创建接口.映射文件和测试类 ++++++++++++++++++++++++++分割线++++++++++++++++++++++++++ 注意namespace属性值为对应接口的全限定类名 ...
- 万字长文硬核AQS源码分析
阅读本文前,需要储备的知识点如下,点击链接直接跳转. java线程详解 Java不能操作内存?Unsafe了解一下 一文读懂LockSupport AQS简介 AQS即AbstractQueuedSy ...
- 如何使用Grid中的repeat函数
在本文中,我们将探索 CSS Grid repeat() 函数的所有可能性,它允许我们高效地创建 Grid 列和行的模式,甚至无需媒体查询就可以创建响应式布局. 不要重复自己 通过 grid-temp ...
- datetime获取当前日期前十二个月份
from dateutil.parser import parse from dateutil.relativedelta import relativedelta # 当前日期前十二个月 time_ ...
- 【题解】AtCoder Beginner Contest 318(D - Ex)
赛时过了 A-G,Ex 仿佛猜到了结论但是完全不懂多项式科技,就炸了. 大家好像都秒了 A,B,C 就不写了. D.General Weighted Max Matching 题目描述: 给你一个加权 ...
- 开源.NetCore通用工具库Xmtool使用连载 - 图形验证码篇
[Github源码] <上一篇> 介绍了Xmtool工具库中的Web操作类库,今天我们继续为大家介绍其中的图形验证码类库. 图形验证码是为了抵御恶意攻击出现的一种设计:例如用户登录.修改密 ...
- 基于百度AI实现文字和图像敏感内容审核
前言 百度AI是指百度公司的人工智能技术全称.它采用深度学习技术,包括自然语言处理.语音识别.计算机视觉.知识图谱等,可应用于各个领域如互联网.医疗.金融.教育.汽车.物流等.百度AI的发展将帮助人类 ...
- IP协议:连接你我,掌握互联网的关键
IP 基本认识 在之前的章节中,我们已经详细介绍了应用层和传输层的相关概念和原理,了解了进程之间如何进行可靠的数据传输.我们知道,传输层的头部包含了进程所使用的端口信息,这是为了确保数据能够正确地传递 ...
- GO语言基础之基本运算符
GO语言基础之基本运算符 目录 GO语言基础之基本运算符 一.运算符 内置运算符: 二.算术运算符 三.关系运算符 四.逻辑运算符 五.位运算符 六.赋值运算符 一.运算符 作用:运算符用于在程序运行 ...
- .NET 8 RC 2 发布,将在11月14日发布正式版
微软2023-10-10 发布了 .NET 8 RC 2,下一站是.NET 8正式发布,就在下个月Net Conf 2023[1](11月14日)期间正式发布,我们也开始筹备第四届中国.NET开发者峰 ...