Problem Statement

Snuke found a random number generator.
It generates an integer between $0$ and $N-1$ (inclusive).
An integer sequence $A_0, A_1, \cdots, A_{N-1}$ represents the probability that each of these integers is generated. The integer $i$ ($0 \leq i \leq N-1$) is generated with probability $A_i / S$, where $S = \sum_{i=0}^{N-1} A_i$. The process of generating an integer is done independently each time the generator is executed.

Now, Snuke will repeatedly generate an integer with this generator until the following condition is satisfied:

  • For every $i$ ($0 \leq i \leq N-1$), the integer $i$ has been generated at least $B_i$ times so far.

Find the expected number of times Snuke will generate an integer, and print it modulo $998244353$.
More formally, represent the expected number of generations as an irreducible fraction $P/Q$. Then, there exists a unique integer $R$ such that $R \times Q \equiv P \pmod{998244353},\ 0 \leq R < 998244353$, so print this $R$.

From the constraints of this problem, we can prove that the expected number of generations is a finite rational number, and its integer representation modulo $998244353$ can be defined.

Constraints

  • $1 \leq N \leq 400$
  • $1 \leq A_i$
  • $\sum_{i=0}^{N-1} A_i \leq 400$
  • $1 \leq B_i$
  • $\sum_{i=0}^{N-1} B_i \leq 400$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$A_0$ $B_0$
$A_1$ $B_1$
$\vdots$
$A_{N-1}$ $B_{N-1}$

Output

Print the expected number of times Snuke will generate an integer, modulo $998244353$.


Sample Input 1

2
1 1
1 1

Sample Output 1

3

The expected number of times Snuke will generate an integer is $3$.


Sample Input 2

3
1 3
2 2
3 1

Sample Output 2

971485877

The expected number of times Snuke will generate an integer is $132929/7200$.


Sample Input 3

15
29 3
78 69
19 15
82 14
9 120
14 51
3 7
6 14
28 4
13 12
1 5
32 30
49 24
35 23
2 9

Sample Output 3

371626143

要求最晚的一个被填满的时间,是很难求的。考虑min-max反演,把他转变为求第一个被填到的时间。

现在要求每一个集合的第一个被填到 \(b_i\) 的时间,枚举这个是那个点,然后我们就知道这个序列中最后一个被填的是哪一个(设为给 d加了1)。那其他点呢?我们需要结束时这个点的状态,把他设为 \(c_i\),那么对于一个固定的 \(c\),在选的顺序上有 \(\frac{(\sum c_i)!}{\prod c_i!}\) 种,刚好会选出这些数的概率是 \(\tfrac{\prod a_i^{c_i}}{S^{\sum c_i}}\),最后刚好选到 d 的概率是 \(\frac dS\)。这些都是集合内的概率,而期望下在 \(\frac S{\sum a_i}\) 会抽到一次集合内的数,则再乘一个 \(\frac S{\sum a_i}(\sum c+1)\)。汇总一下式子

\[\dfrac{(\sum c_i)!}{\prod c_i!}\times \dfrac{\prod a_i^{c_i}}{S^{\sum c_i}}\times \dfrac dS\times \dfrac{\prod a_i^{c_i}}{S^{\sum c_i}}\times \dfrac S{\sum a_i}(\sum c+1)
\]

到这里就可以dp了,定义 \(dp_{i,j,k,0/1,0/1}\) 为集合中前 \(i\) 个数,c之和为 j,a之和为k,容斥系数和是否选出d

对于某一个集合,我们需要dp才能解决,那么对于所有的集合,我们也可以通过dp来解决,只不过多了一种不选入集合的情况

虽然枚举加 dp 是四维的,但是 \(\sum b\le 400\),所以总共也是 \(n^3\)

#include<bits/stdc++.h>
using namespace std;
const int N=405,P=998244353;
int n,a[N],b[N],f[N],iv[N],dp[N][N][2][2],sa,sb,inv[N],ans;
int pown(int x,int y)
{
if(!y)
return 1;
int t=pown(x,y>>1);
if(y&1)
return 1LL*t*t%P*x%P;
return 1LL*t*t%P;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",a+i,b+i);
for(int i=inv[1]=iv[0]=f[0]=1;i<=400;i++)
{
f[i]=1LL*f[i-1]*i%P;
if(i^1)
inv[i]=1LL*(P-P/i)*inv[P%i]%P;
iv[i]=1LL*iv[i-1]*inv[i]%P;
}
dp[0][0][0][0]=1;
for(int i=1;i<=n;i++)
{
sa+=a[i];
sb+=b[i];
for(int j=sa;j>=a[i];j--)
{
for(int k=sb;k>=0;k--)
{
long long pw=1,lp;
for(int c=0;c<b[i]&&c<=k;c++,pw=1LL*pw*a[i]%P)
{
lp=pw;
(dp[j][k][0][0]+=dp[j-a[i]][k-c][1][0]*pw%P*iv[c]%P)%=P;
(dp[j][k][1][0]+=dp[j-a[i]][k-c][0][0]*pw%P*iv[c]%P)%=P;
(dp[j][k][1][1]+=dp[j-a[i]][k-c][0][1]*pw%P*iv[c]%P)%=P;
(dp[j][k][0][1]+=dp[j-a[i]][k-c][1][1]*pw%P*iv[c]%P)%=P;
}
if(k>=b[i]-1)
{
(dp[j][k][0][1]+=pw*dp[j-a[i]][k-b[i]+1][1][0]%P*iv[b[i]-1]%P)%=P;
(dp[j][k][1][1]+=pw*dp[j-a[i]][k-b[i]+1][0][0]%P*iv[b[i]-1]%P)%=P;
}
}
}
}
for(int i=0;i<=sa;i++)
{
for(int j=0;j<=sb;j++){
(ans+=1LL*(P-1)*dp[i][j][0][1]%P*(j+1)%P*inv[i]%P*f[j]%P*inv[i]%P*pown(pown(i,j),P-2)%P)%=P;
(ans+=1LL*dp[i][j][1][1]*(j+1)%P*inv[i]%P*f[j]%P*inv[i]%P*pown(pown(i,j),P-2)%P)%=P;
}
}
printf("%lld\n",ans*1LL*sa%P);
}

[AGC038E] Gachapon的更多相关文章

  1. @atcoder - AGC038E@ Gachapon

    目录 @description@ @solution - 1@ @accepted code - 1@ @solution - 2@ @accepted code - 2@ @details@ @de ...

  2. AtCoder Grand Contest 038E - Gachapon

    \(\bf Description\) 一个 \(0\) 到 \(n-1\) 的随机数生成器,生成 \(i\) 的概率是 \(A_i/S\) ,其中 \(S=\sum_{i=0}^{n} A_i\) ...

  3. Atcoder Grand Contest 038 E - Gachapon(Min-Max 容斥+背包)

    Atcoder 题面传送门 & 洛谷题面传送门 我竟然能独立做出 Ag 的 AGC E,incredible!更新了 Atcoder 做题难度上限( 首先按照套路 Min-Max 容斥,\(a ...

  4. SFC游戏列表(维基百科)

    SFC游戏列表 日文名 中文译名 英文版名 发行日期 发行商 スーパーマリオワールド 超级马里奥世界 Super Mario World 1990年11月21日 任天堂 エフゼロ F-Zero F-Z ...

  5. FC红白机游戏列表(维基百科)

    1055个fc游戏列表 日文名 中文译名 英文版名 发行日期 发行商 ドンキーコング 大金刚 Donkey Kong 1983年7月15日 任天堂 ドンキーコングJR. 大金刚Jr. Donkey K ...

  6. AtCoder Grand Contest 038 简要题解

    从这里开始 比赛目录 Problem A 01 Matrix Code #include <bits/stdc++.h> using namespace std; typedef bool ...

  7. AtCoder Grand Contest 038题解

    好久没更了 写点东西吧= = A 01Matrix 简单构造 左上角和右下角染成1其他染成0即可 #include<bits/stdc++.h> #define ll long long ...

  8. 咕咕咕清单(SCOI2020前)

    本篇博客已停更 本篇博客已停更 本篇博客已停更 吐槽区: 2020.04.15: 从今天起我做过的题目都记录一下,想不想写题解就另说了 2020.04.17: 写了两天之后真实的发现这是博主的摸鱼日记 ...

  9. AtCoder随做

    突然发现只有我没写过 AT. 没写题解不意味着没做,有的忘了写或者太草率了就算了. 部分前言删了. 目录 ABC020D ABC241G ABC268 AGC003D AGC004D AGC004E ...

随机推荐

  1. ETL之apache hop系列1-ETL概念与hop简介

    ETL 简单介绍 ETL概念 ETL,是英文Extract-Transform-Load的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的 ...

  2. Kafka-基础

    1. 简介 Kafka(Apache Kafka) 是一种分布式流数据平台,最初由LinkedIn开发,并于后来捐赠给Apache软件基金会,成为了一个Apache顶级项目.它被设计用于处理大规模.实 ...

  3. 淘宝详情api接口的使用说明

    淘宝详情API接口是一种可以用来获取淘宝商品详细信息的服务,包括图片.标题.价格.销量.评论等数据.下面是淘宝详情API接口的使用说明: 1.关于申请API接口权限: 在使用淘宝详情API接口前,需要 ...

  4. 【源码】Vue.js 官方脚手架 create-vue 是怎么实现的?

    Vue.js 官方脚手架 create-vue 是怎么实现的? 摘要 本文共分为四个部分,系统解析了vue.js 官方脚手架 create-vue 的实现细节. 第一部分主要是一些准备工作,如源码下载 ...

  5. MySQL实战实战系列 02 日志系统:一条SQL更新语句是如何执行的?

    前面我们系统了解了一个查询语句的执行流程,并介绍了执行过程中涉及的处理模块.相信你还记得,一条查询语句的执行过程一般是经过连接器.分析器.优化器.执行器等功能模块,最后到达存储引擎. 那么,一条更新语 ...

  6. ​python爬虫——爬取天气预报信息

    在本文中,我们将学习如何使用代理IP爬取天气预报信息.我们将使用 Python 编写程序,并使用 requests 和 BeautifulSoup 库来获取和解析 HTML.此外,我们还将使用代理服务 ...

  7. 其它——各主流Linux系统解决pip安装mysqlclient报错

    文章目录 一 CentOS(红帽) 二 Ubuntu 三 Windows 一 CentOS(红帽) #CentOS有Python.Mysql的开发工具包,安装后使用pip安装mysqlclient即可 ...

  8. 算法解析:LeetCode——机器人碰撞和最低票价

    摘要:本文由葡萄城技术团队原创.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 机器人碰撞 问题: 现有 n 个机器人,编号从 1 开始,每个机器人包含在路 ...

  9. 铅华洗尽,粉黛不施,人工智能AI基于ProPainter技术去除图片以及视频水印(Python3.10)

    视频以及图片修复技术是一项具有挑战性的AI视觉任务,它涉及在视频或者图片序列中填补缺失或损坏的区域,同时保持空间和时间的连贯性.该技术在视频补全.对象移除.视频恢复等领域有广泛应用.近年来,两种突出的 ...

  10. 02-oracle11g rac RMAN备份恢复至单机(未验证)

    在一节点上进行全备确定备份路径,并赋予属组mkdir /rmanbackupchown oracle:oinsatll /rmanbackup进入rman进行全备rman target /run{al ...