[ABC263E] Sugoroku 3
Problem Statement
There are squares called Square though Square . You start on Square .
Each of the squares from Square through Square has a die on it. The die on Square is labeled with the integers from through , each occurring with equal probability. (Die rolls are independent of each other.)
Until you reach Square , you will repeat rolling a die on the square you are on. Here, if the die on Square rolls the integer , you go to Square .
Find the expected value, modulo , 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 using two coprime integers and , there is a unique integer such that and . Find this .
Constraints
- 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 , so should be printed.
Here is one possible scenario until reaching Square :
- Roll on Square , and go to Square .
- Roll on Square , and stay there.
- Roll on Square , and go to Square .
This scenario occurs with probability .
Sample Input 2
5 3 1 2 1
发现正着定义状态很难定义。定义 表示从第 个点到达第 个点的期望步数。
那么可以列出
对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]);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!