牛客多校第9场B(前缀和与差分优化dp)

思路:

设计状态:最开始我是将f(i, j)表示为走到第i格走了j步,但是这样转移f(i, j) = ∑f(i - a(k))(j - 1) * 1 / a(k).O(n^3).这样貌似没办法优化,因为枚举的顺序取决于外层.于是我们调换顺序f(i, j)表示走了i格走到第j步,这样f(i + 1, j + 1 + ...) = f(i, j) * 1 / a(j),这样就能用前缀和加差分优化了.

代码:

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
#define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f 
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 8010, MOD = 998244353;
int n, a[N], f[N][N], inv[N];
signed main() {
	qio >> n;
	inv[1] = 1;
	for (int i = 2; i <= n; ++i) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
	for (int i = 1; i <= n - 1; ++i) qio >> a[i];
	//f[i][j]表示跳了i步到了第j个位置,f[i + 1][j + 1...a[i]] = f[i][j] * 1 / a[i]
	//使用前缀和优化
	f[0][1] += 1, f[0][2] -= 1;
	int ans = 0;
	for (int i = 0; i <= n - 1; ++i) {
		for (int j = 1; j <= n; ++j) f[i][j] = (f[i][j] + f[i][j - 1]) % MOD;
		ans = (ans + f[i][n] * f[i][n] % MOD) % MOD;
		for (int j = 1; j <= n; ++j) {
			int p = f[i][j] * inv[a[j]] % MOD;
			f[i + 1][j + 1] = (f[i + 1][j + 1] + p) % MOD;
			f[i + 1][j + a[j] + 1] = (f[i + 1][j + a[j] + 1] - p) % MOD;
		}
	}
	qio << ans << "\n";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值