(AtCoder Beginner Contest 253)E - Distance Sequence
一个很容易看出的 ,不过需要优化一下状态转移过程
- 前缀和优化 复杂度
// Problem: E - Distance Sequence
// Contest: AtCoder - NOMURA Programming Contest 2022(AtCoder Beginner Contest 253)
// URL: https://atcoder.jp/contests/abc253/tasks/abc253_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(int i(a); i <= b; i ++)
#define dec(i, a, b) for(int i(a); i >= b; i --)
#ifdef LOCAL
#include <debugger>
#else
#define debug(...) 42
#endif
template <typename T> inline void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> inline void chkmin(T &x, T y) { x = min(x, y); }
constexpr int mod = 998244353;
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] = (fenw[x] + v) % mod;
if(fenw[x] < 0) fenw[x] += mod;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x]; v %= mod;
if(v < 0) v += mod;
x = (x & (x + 1)) - 1;
}
return v;
}
};
void solve() {
int n, m, k; cin >> n >> m >> k;
vector<ll> f(m + 1);
vector<ll> s(m + 1);
rep(i, 1, m) f[i] = 1, s[i] = s[i - 1] + f[i];
rep(i, 2, n) {
rep(j, 1, m) {
if(j > k) {
f[j] = (k == 0 ? s[j - k - 1] : s[j - k]);
} else {
f[j] = 0;
}
if(j + k <= m) {
f[j] = (f[j] + (s[m] - s[j + k - 1])) % mod;
if(f[j] < 0) f[j] += mod;
}
}
rep(j, 1, m) {
s[j] = (s[j - 1] + f[j]) % mod;
}
}
cout << s.back();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
/*
*
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
- 树状数组优化 复杂度
// Problem: E - Distance Sequence
// Contest: AtCoder - NOMURA Programming Contest 2022(AtCoder Beginner Contest 253)
// URL: https://atcoder.jp/contests/abc253/tasks/abc253_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(int i(a); i <= b; i ++)
#define dec(i, a, b) for(int i(a); i >= b; i --)
#ifdef LOCAL
#include <debugger>
#else
#define debug(...) 42
#endif
template <typename T> inline void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> inline void chkmin(T &x, T y) { x = min(x, y); }
constexpr int mod = 998244353;
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] = (fenw[x] + v) % mod;
if(fenw[x] < 0) fenw[x] += mod;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x]; v %= mod;
if(v < 0) v += mod;
x = (x & (x + 1)) - 1;
}
return v;
}
};
void solve() {
int n, m, k; cin >> n >> m >> k;
vector<ll> f(m + 1);
rep(i, 1, m) f[i] = 1;
rep(i, 2, n) {
fenwick<ll> fen(m + 10);
rep(j, 1, m) {
if(!k) {
fen.modify(1, f[j]);
} else {
if(j > k) {
fen.modify(1, f[j]);
fen.modify(j - k + 1, -f[j]);
}
if(j + k <= m) {
fen.modify(j + k, f[j]);
}
}
}
rep(j, 1, m) {
f[j] = fen.get(j);
}
}
ll ans = 0;
rep(i, 1, m) {
ans += f[i];
ans %= mod;
if(ans < 0) ans += mod;
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
/*
*
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现