[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数据类型 /*------------------------------------------------------------------------------+ #| = : = : ...
随机推荐
- Linux虚拟机报错Job for network.service failed because the control process exited with error codeLinux虚拟机报错的解决方法
发布于 2 天前 3 次阅读 Linux虚拟机设置静态ip后,突然发现联网连不上了,ssh也无法使用,重启network后仍旧无法使用.按照网络上的方法发现没有效果后,右键如下位置将nat模式转换为 ...
- Unity UGUI的Image(图片)组件的介绍及使用
UGUI的Image(图片)组件的介绍及使用 1. 什么是UGUI的Image(图片)组件? UGUI的Image(图片)组件是Unity引擎中的一种UI组件,用于显示2D图像.它提供了一种简单而灵活 ...
- 玩转 PI 系列-看起来像服务器的 ARM 开发板矩阵-Firefly Cluster Server
前言 基于我个人的工作内容和兴趣,想要在家里搞一套服务器集群,用于容器/K8s 等方案的测试验证. 考虑过使用二手服务器,比如 Dell R730, 还搞了一套配置清单,如下: Dell R730 3 ...
- Python 基础面试第三弹
1. 获取当前目录下所有文件名 import os def get_all_files(directory): file_list = [] # os.walk返回一个生成器,每次迭代时返回当前目录路 ...
- 浅谈基于QT的截图工具的设计与实现
本人一直在做属于自己的一款跨平台的截图软件(w4ngzhen/capi(github.com)),在软件编写的过程中有一些心得体会,所以有了本文.其实这篇文章酝酿了很久,现在这款软件有了雏形,也有空梳 ...
- Unity UGUI的Scrollbar(滚动条)组件的介绍及使用
Unity UGUI的Scrollbar(滚动条)组件的介绍及使用 一.什么是Scrollbar组件? Scrollbar组件是Unity中UGUI系统提供的一种UI组件,主要用于在UI界面中提供滚动 ...
- [初学C#] 第二习题 : 快递跟踪信息查询
刚学C#, 折腾的一个小玩意. 熟悉和了解C#这门编程语言. 没有啥特殊意义 解锁技能 - System.Net 的 WebRequest等http请求 - Newtonsoft.Json 这个第三方 ...
- idea如何显示出分支名
如图所示 配置修改 idea安装目录下bin/idea.properties文件,新增2行配置 project.tree.structure.show.url=false ide.tree.horiz ...
- JUC并发编程(2)—synchronized锁原理
目录 乐观锁和悲观锁介绍 synchronized用法介绍 synchronized和ReentrantLock的区别 经典8锁问题案例 从字节码角度分析synchronized实现 synchron ...
- 23年9月最新微信小程序 手机号授权 (uniapp+盛派SDK) 帮你踩坑
一.背景 微信小程序手机号授权接口,从23年8月开始实行付费验证. 文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/op ...