[ABC263E] Sugoroku 3

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 N1 has a die on it. The die on Square i is labeled with the integers from 0 through Ai, 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 PQ using two coprime integers P and Q, there is a unique integer R such that R×QP(mod998244353) and 0R<998244353. Find this R.

Constraints

  • 2N2×105
  • 1AiNi(1iN1)
  • 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 18.


Sample Input 2

5
3 1 2 1

发现正着定义状态很难定义。定义 dpi 表示从第 i 个点到达第 n 个点的期望步数。
那么可以列出 dpi=1ai+1dpi+1ai+1dpi+1+1ai+1dpi+ai+1

aiai+1dpi=1ai+1dpi+1+1ai+1dpi+ai+1

dpi=1ai(dpi+1+dpi+2+...+dpi+ai)+ai+1·ai

对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]);
}
posted @   灰鲭鲨  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示