题解:CF1009E Intercity Travelling

· · 题解

推式子题,其实打表也能看出一些规律。已使用格式化机器整理格式。

::::info[题意简述]{open} 从 0 到 n 有 n 千米。在 1,2,\dots,n-1 每个整数点,可能有休息站,共有 2^{n-1} 种方案,等概率。

求所有方案总难度之和,对 998244353 取模。

对于这种期望题,一个经典的做法就是把所有 a_i 拆开分别计算。首先考虑对于第 i 个点的贡献是 a_i 的情况,即从头到尾没有休息,也就是说 1,2,\dots,i-1 的地方没有休息站,其他地方没有约束,所以方案数为 2^{n-i},这种情况会出现一次,贡献为 a_i \times 2^{n-i}。

接下来考虑休息站在 j,且从 j 开始后连续行驶了 i 千米的情况,那么则需要满足:

其他位置没有约束,所以方案数为 2^{n-i-1},这种情况会出现 (n-i) 次,总贡献为 a_i \times (n-i) \times 2^{n-i-1}。

那么把两部分合到一块,答案为:

a_n + \sum_{i=1}^{n-1} a_i \left(2^{n-i} + (n-i)2^{n-i-1}\right)

整理得:

a_n + \sum_{i=1}^{n-1} a_i \cdot (n-i+2) \cdot 2^{n-i-1}

时间复杂度 O(n)。

::::success[AC code]

/*

time : 2026-07-30 16:22:00

contest :

Problem : 

Solution :

Code by 720872

*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<climits>
#include<bitset>
#define ll long long
#define ull unsigned long long
#define il inline
#define endl '\n'
#define y1 Y1
#define pii pair<int,int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define _1 first
#define _2 second
#define mkp make_pair
#define pb push_back
#define lowbit(x) x & (-x)
#define debug cout << "debug" << endl;
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
using namespace std;

inline ll read(){
    ll k = 0, f = 1, c = gc();
    for (; !isdigit(c); c = gc()) if (c == '-') f = -1;
    for (; isdigit(c); c = gc()) k = k * 10 + (c ^ 48);
    return k * f;
}
//inline void put(ll x){
//  if(x < 0) putchar('-'), x = -x;
//  if(x < 10) putchar(x + '0');
//  else put(x / 10), putchar(x % 10 + '0');
//}

const ll mod = 998244353, N = 1e6 + 5;
ll p[N], ans, a[N], n;

signed main(){
//  freopen(".in", "r", stdin);
//  freopen(".out", "w", stdout);
//  ios::sync_with_stdio(false);
//  cin.tie(0); cout.tie(0);
    n = read();
    p[0] = 1;
    for(int i = 1; i <= n; i++)
        a[i] = read(), p[i] = (p[i - 1] * 2) % mod;
        for(int i = 1; i < n; i++){ //注意当 i=n 时会造成越界,此时只需要最后加上 a[n] 即可。
        ans = (ans + (a[i] * (n - i + 2) % mod) * p[n - i - 1] % mod) % mod;
    }
    cout << (ans + a[n]) % mod << endl;
    return 0;
}
/*
*/

::::