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. Pytorch语法——torch.autograd.grad

    The torch.autograd.grad function is a part of PyTorch's automatic differentiation package and is use ...

  2. WPF使用TextBlock实现查找结果高亮显示

    在应用开发过程中,经常遇到这样的需求:通过关键字查找数据,把带有关键字的数据显示出来,同时在结果中高亮显示关键字.在web开发中,只需在关键字上加一层标签,然后设置标签样式就可以轻松实现. 在WPF中 ...

  3. GitHub Actions CI/CD 工作流实战

    1. 什么是 GitHub Actions 与 workflow ? GitHub Actions 是 GitHub 提供的一种持续集成(CI)和持续部署(CD)的工具,用于自动化软件开发过程中的各种 ...

  4. github.com/yuin/gopher-lua 踩坑日记

    本文主要记录下在日常开发过程中, 使用 github.com/yuin/gopher-lua 过程中需要注意的地方. 后续遇到其他的需要注意的事项再补充. 1.加载LUA_PATH环境变量 在实际开发 ...

  5. 应用程序通过 Envoy 代理和 Jaeger 进行分布式追踪 —— Ingress Controller + Http服务 + Grpc服务(三)

    1.概述 在<应用程序通过 Envoy 代理和 Jaeger 进行分布式追踪(一)>这篇博文中,我们详细介绍了单个应用程序通过 Envoy 和 Jaeger 实现链路追踪的过程.通过这个示 ...

  6. SpringBoot 后端配置 Https 教程

    以阿里云为例子 1. 申请 SSL 证书 1. 注册域名 打开阿里云官网,搜索域名 点击域名注册,输入域名,点击搜索 选择心仪的域名,点击购买,打钱 进入域名控制台,进行实名认证 2. 申请 SSL ...

  7. Solution Set -「CF 1525」

    「CF 1525A」Potion-making Link. 显然. #include<bits/stdc++.h> typedef long long ll; template<ty ...

  8. Solution -「九省联考 2018」劈配

    Description Link. 一年一度的综艺节目<中国新代码>又开始了.Zayid 从小就梦想成为一名程序员,他觉得这是一个展示自己的舞台,于是他毫不犹豫地报名了. 轻车熟路的 Za ...

  9. JAVA中三种I/O框架——BIO、NIO、AIO

    一.BIO(Blocking I/O) BIO,同步阻塞IO模型,应用程序发起系统调用后会一直等待数据的请求,直至内核从磁盘获取到数据并拷贝到用户空间: 在一般的场景中,多线程模型下的BIO是成本较低 ...

  10. DHCP是什么

    DHCP 1. DHCP是什么 协议,一种应用层的网络协议,他可以动态地分配网络中的IP地址和其他网络配置的参数以及网络设备,通俗一点讲,每台设备的IP地址,子网掩码,网关等网络参数信息都是由他来完成 ...