E - Sugoroku 3(期望dp)

Posted Harris-H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E - Sugoroku 3(期望dp)相关的知识,希望对你有一定的参考价值。

E - Sugoroku 3(期望dp)

d p i = ∑ j = i i + a i d p j a i + 1 + 1 dp_i=\\dfrac\\sum_j=i^i+a_idp_ja_i+1+1 dpi=ai+1j=ii+aidpj+1

右边的 d p i dp_i dpi 移项得到:

a i a i + 1 d p i = ∑ j = i + 1 i + a i d p j a i + 1 + 1 \\dfraca_ia_i+1dp_i=\\dfrac\\sum_j=i+1^i+a_idp_ja_i+1+1 ai+1aidpi=ai+1j=i+1i+aidpj+1

d p i = ∑ j = i + 1 i + a i d p j a i + a i + 1 a i = ( ∑ j = i + 1 i + a i d p j ) + a i + 1 a i dp_i=\\dfrac\\sum_j=i+1^i+a_idp_ja_i+\\dfraca_i+1a_i=\\dfrac(\\sum_j=i+1^i+a_idp_j)+a_i+1a_i dpi=aij=i+1i+aidpj+aiai+1=ai(j=i+1i+aidpj)+ai+1

取模用逆元递推即可。

时间复杂度: O ( n ) O(n) O(n)

// Problem: E - Sugoroku 3
// Contest: AtCoder - LINE  Verda Programming Contest(AtCoder Beginner Contest 263)
// URL: https://atcoder.jp/contests/abc263/tasks/abc263_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Date: 2022-08-09 12:44:54
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=2e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=998244353;
const int hashmod[4] = 402653189,805306457,1610612741,998244353;
#define mst(a,b) memset(a,b,sizeof a)
#define db double
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define ios ios::sync_with_stdio(false),cin.tie(nullptr) 
void Print(int *a,int n)
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\\n",a[n]); 

template <typename T>		//x=max(x,y)  x=min(x,y)
void cmx(T &x,T y)
	if(x<y) x=y;

template <typename T>
void cmn(T &x,T y)
	if(x>y) x=y;

int n;
ll dp[N];
ll suf[N];
ll a[N];
ll inv[N];
ll p=mod;
void init(int n)
	inv[1]=1;
	for(int i=2;i<=n;i++) 
	inv[i]=(p-p/i)*inv[p%i]%p;

int main()
	ll q = 1;
	cin>>n;
	rep(i,1,n-1) cin>>a[i];
	init(n);
	dp[n] = 0;
	for(int i=n-1;i;i--)
		dp[i] = (suf[i+1]-suf[i+a[i]+1]+a[i]+1)*inv[a[i]]%mod;
		suf[i] = (suf[i+1] + dp[i])%mod;
	
	printf("%lld",(dp[1]%mod+mod)%mod);
	return 0;


以上是关于E - Sugoroku 3(期望dp)的主要内容,如果未能解决你的问题,请参考以下文章

E - Sugoroku 4(概率dp)

数学期望dp

HDU 4035 Maze(树形概率DP)

期望与概率dp

一天一DP计划概率期望DP

cf643E. Bear and Destroying Subtrees(期望dp)