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. typora使用教程&高级用法&Markdown

    typora使用教程&高级用法&Markdown typora介绍 哇啦哇啦哇啦哇,,,,,,,,,,,,, 提示:小白看不懂的话,建议哔哩哔哩搜索"遇见狂神说", ...

  2. Linux-源码安装软件

    一.源码安装步骤 源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). 1.配置(configure) Configure是一个可执行脚本,它 ...

  3. xxl-job初学转载,不断更新

    参考:https://blog.csdn.net/xhmico/article/details/122324950 官网与源码下载地址 官网:https://www.xuxueli.com/xxl-j ...

  4. Python爬虫-IP隐藏技术与代理爬取

    在进行爬虫程序开发和运行时,常常会遇到目标网站的反爬虫机制,最常见的就是IP封禁,这时需要使用IP隐藏技术和代理爬取. 一.IP隐藏技术 IP隐藏技术,即伪装IP地址,使得爬虫请求的IP地址不被目标网 ...

  5. 【Python爬虫实战】爬虫封你ip就不会了?ip代理池安排上

    前言 在进行网络爬取时,使用代理是经常遇到的问题.由于某些网站的限制,我们可能会被封禁或者频繁访问时会遇到访问速度变慢等问题.因此,我们需要使用代理池来避免这些问题.本文将为大家介绍如何使用IP代理池 ...

  6. 5分钟入门 next13

    上半年vercel 推出了nextjs13 这个大版本,刚好最近有个c端的项目,所以就用了这个框架来写,技术体系基本也是文档提到的 tailwindcss + ts + swr + ssr ,总的来开 ...

  7. ora2pg使用记录

    ora2pg使用记录 前言 这篇文章是我在学习使用ora2pg过程中的学习记录,以便日后遗忘查阅: 诸君也可跟随我的步伐了解一下ora2pg,或可移步如下官方文档参考学习:Ora2Pg : Migra ...

  8. Cplex混合整数规划求解(Python API)

    绝对的原创!罕见的Cplex-Python API混合整数规划求解教程!这是我盯了一天的程序一条条写注释一条条悟出来的•́‸ก 一.问题描述 求解有容量限制的的设施位置问题,使用Benders分解.模 ...

  9. Aoba's GitLab Doki Theme - 一个简单的 GitLab 主题工具

    前言 平常工作在用 GitLab 但总觉得缺点什么颜色好单调,于是随手摸了一个主题工具 界面预览 GitLab 主页效果 个人偏好配置页面 安装方法 安装 Tampermonkey 之类的用户脚本工具 ...

  10. math库常用函数+产生随机数总结

    math库常用函数+产生随机数总结 1.对x开平方 double sqrt(x)://返回值为double类型,输入的x类型随意,只要是数的类型 2.求常数e的x次方 double exp(x);// ...