Problem Statement

There are $N$ squares called Square $1$ though Square $N$. You start on Square $1$.

Each of the squares from Square $1$ through Square $N-1$ has a die on it. The die on Square $i$ is labeled with the integers from $0$ through $A_i$, each occurring with equal probability. (Die rolls are independent of each other.)

Until you reach Square $N$, you will repeat rolling a die on the square you are on. Here, if the die on Square $x$ rolls the integer $y$, you go to Square $x+y$.

Find the expected value, modulo $998244353$, of the number of times you roll a die.

Notes

It can be proved that the sought expected value is always a rational number. Additionally, if that value is represented $\frac{P}{Q}$ using two coprime integers $P$ and $Q$, there is a unique integer $R$ such that $R \times Q \equiv P\pmod{998244353}$ and $0 \leq R \lt 998244353$. Find this $R$.

Constraints

  • $2 \le N \le 2 \times 10^5$
  • $1 \le A_i \le N-i(1 \le i \le N-1)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\dots$ $A_{N-1}$

Output

Print the answer.


Sample Input 1

3
1 1

Sample Output 1

4

The sought expected value is $4$, so $4$ should be printed.

Here is one possible scenario until reaching Square $N$:

  • Roll $1$ on Square $1$, and go to Square $2$.
  • Roll $0$ on Square $2$, and stay there.
  • Roll $1$ on Square $2$, and go to Square $3$.

This scenario occurs with probability $\frac{1}{8}$.


Sample Input 2

5
3 1 2 1

发现正着定义状态很难定义。定义 \(dp_i\) 表示从第 \(i\) 个点到达第 \(n\) 个点的期望步数。

那么可以列出 $$dp_i=\frac{1}{a_i+1} dp_i+\frac{1}{a_i+1} dp_{i+1}\cdots+\frac{1}{a_i+1} dp_{i+a_i}+1$$

\[\frac{a_i}{a_i+1}dp_i=\frac{1}{a_i+1} dp_{i+1}\cdots+\frac{1}{a_i+1} dp_{i+a_i}+1
\]
\[dp_i=\frac{1}{a_i}(dp_{i+1}+dp_{i+2}+...+dp_{i+a_i})+\frac{a_i+1·}{a_i}
\]

对dp数组维护后缀和即可。

#include<cstdio>
const int N=2e5+5,P=998244353;
int dp[N],a[N],n,add;
long long s[N],ret;
int pow(int x,int y)
{
if(!y)
return 1;
int t=pow(x,y>>1);
return y&1? 1LL*t*t%P*x%P:1LL*t*t%P;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++)
scanf("%d",a+i);
for(int i=n-1;i>=1;i--)
{
// scanf("%d",a+i);
dp[i]=1LL*(s[i+1]-s[i+a[i]+1]+P)%P*pow(a[i],P-2)%P+1LL*(a[i]+1)*pow(a[i],P-2)%P ;
dp[i]%=P;
// printf("%",dp[i]);
s[i]=s[i+1]+dp[i];
s[i]%=P;
}
printf("%d",dp[1]);
}

[ABC263E] Sugoroku 3的更多相关文章

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

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

  2. ARC145~152 题解

    比赛标号从大到小排列 . 因为博主比较菜所以没有题解的题都是博主不会做的 /youl ARC144 以前的比赛懒得写了 . 目录 AtCoder Regular Contest 152 B. Pass ...

  3. ARC149(A~E)

    Tasks - AtCoder Regular Contest 149 又是114514年前做的题,现在来写 屯了好多,清一下库存 A - Repdigit Number (atcoder.jp) 直 ...

随机推荐

  1. 你的开发套件已到货「GitHub 热点速览」

    这周的 GitHub 热点榜,撇开上周的介绍过的几个项目,剩下就两字:套件.像是搜罗了大量黑客工具的 hackingtool,还有打算一统米哈游游戏客户端的 Starward,以及好用的 CV 库 s ...

  2. 千万级数据深分页查询SQL性能优化实践

    一.系统介绍和问题描述 如何在Mysql中实现上亿数据的遍历查询?先来介绍一下系统主角:关注系统,主要是维护京东用户和业务对象之前的关注关系:并对外提供各种关系查询,比如查询用户的关注商品或店铺列表, ...

  3. 【LaTeX】环境配置以及中文支持

    目录 网页环境 Overleaf 本地环境 TeX Live TeXstudio VSCode 安装 LaTeX Workshop 扩展 编译链配置 正向同步 反向同步 其他可选配置 中文支持 XeL ...

  4. Mysql高阶自定义排序

    Mysql高阶自定义排序 嗨,大家好,我是远码,隔三岔五给大家分享一点工作的技术总结,花费的时间不多,几分钟就行,谢谢! Mysql对我们码农来说是在熟悉不过的日常了,就不在介绍它的基础用法了,今天我 ...

  5. 一文了解Validator库

    1. 引言 github.com/go-playground/validator 是一个 Go 语言的库,用于对结构体字段进行验证.它提供了一种简单而灵活的方式来定义验证规则,并在验证过程中检查结构体 ...

  6. 问题总结:浮点数之间的等值判断,基本数据类型不能用==来比较,包装数据类型不能用equals来判断

    浮点数之间的等值判断,基本数据类型不能用==来比较,包装数据类型不能用equals来判断. 说明:浮点数采用"尾数+阶码"的编码方式,类似于科学计数法的"有效数字+指数& ...

  7. 【krpano】密码插件

    密码插件可以在浏览场景或者执行action之前弹出密码输入框,要求用户输入密码.当密码输入成功了才可以进行下一步操作. 下载地址:http://pan.baidu.com/s/1gfOKKKF 给场景 ...

  8. 洛谷题解 | AT_abc321_c Primes on Interval

    目录 题目翻译 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 样例 #2 样例输入 #2 样例输出 #2 样例 #3 样例输入 #3 样例输出 #3 题目简化 题目思路 A ...

  9. 使用SemanticKernel 进行智能应用开发(2023-10更新)

    以OpenAI 的ChatGPT 所掀起的GenAI 快速创新浪潮,其中连接LLM 和 应用之间的桥梁的两大开源项目:LangChain[1]和Semantic Kernel[2] ,在半年前写过一篇 ...

  10. Python 列表操作指南3

    示例,将新列表中的所有值设置为 'hello': newlist = ['hello' for x in fruits] 表达式还可以包含条件,不像筛选器那样,而是作为操纵结果的一种方式: 示例,返回 ...