CF1009E Intercity Travelling
题意:
有一段路程,路程可以看作是从0到n的一条直线
如果从起点出发或者从休息点出发,连续驾驶k千米,则需要消耗的体能为a1+…+ak
每个整点都有可能拥有一个休息点,每个休息点存在或者不存在的概率想等的。
求整个旅程消耗的体能的期望为p,输出p∗2n−1p*2^{n-1}p∗2n−1
题解:
最后别忘乘2n−12^n-12n−1
代码:
#include <bits/stdc++.h>
#define LL long long
#define MAXN 1000005
#define P int(998244353)
using namespace std;int N;
LL a[MAXN], s[MAXN];
LL pw[MAXN];
LL dp[MAXN], s1[MAXN], s2[MAXN];/*
dp[i] = (s[i] + sum{(dp[j]+s[i-j])*2^(j-1)})/2^(i-1), j = 1...i-1= (s[i] + sum{dp[j]*2^(j-1)} + 2^(i-1)*sum{s[i-j]/2^(i-j)})/2^(i-1)= s[i]/2^(i-1) + sum{dp[j]*2^j}/2^i + sum{s[j]/2^j}, j=1...i-1
*/LL qpow(LL x, int n){LL res = 1;while(n){if(n & 1) res = res * x % P;x = x * x % P;n /= 2;}return res;
}int main(){scanf("%d", &N);pw[0] = 1;for(int i=1;i<=N;i++){scanf("%d", &a[i]);s[i] = (s[i-1] + a[i]) % P;pw[i] = pw[i-1] * 2 % P;}for(int i=1;i<=N;i++){dp[i] = (s[i]*qpow(pw[i-1], P-2)%P + s1[i-1]*qpow(pw[i], P-2)%P + s2[i-1]%P)%P;s1[i] = (s1[i-1] + dp[i]*pw[i]%P)%P;s2[i] = (s2[i-1] + s[i]*qpow(pw[i],P-2)%P)%P;}printf("%lld\n", dp[N]*pw[N-1]%P);return 0;
}