ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Sugoroku 3

2022-09-12 22:34:29  阅读:288  来源: 互联网

标签:le frac int die Sugoroku Square dp


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]);
}

标签:le,frac,int,die,Sugoroku,Square,dp
来源: https://www.cnblogs.com/mekoszc/p/16687518.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有