chenfy27的刷题记录

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

abc253E 相邻元素之差不低于K的序列数

给定n,m,k,找一个序列A[n],使用满足1<=A[i]<=m,并且任意相邻两元素的差的绝对值大于等于k,求满足条件的序列个数,结果对998244353取模。
2<=n<=1000; 1<=m<=5000; 0<=k<=m-1

设dp[i][j]表示前i个数以j结尾的方案数,在计算dp[i+1][k]时,可以枚举j进行统计,复杂度为O(n^3),可以通过前缀和优化成O(n^2),再用滚动数组,将空间复杂度从O(n^2)优化到O(n)。注意,需要特判k=0的情况。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)
template<int MOD>
struct MInt {
int x;
int norm(int u) const {u%=MOD; if(u<0) u+=MOD; return u;}
MInt(int v=0):x(norm(v)) {}
int val() const {return x;}
MInt operator-() const {return MInt(norm(MOD-x));}
MInt inv() const {assert(x!=0); return power(MOD-2);}
MInt &operator*=(const MInt &o) {x=norm(x*o.x); return *this;}
MInt &operator+=(const MInt &o) {x=norm(x+o.x); return *this;}
MInt &operator-=(const MInt &o) {x=norm(x-o.x); return *this;}
MInt &operator/=(const MInt &o) {*this *= o.inv(); return *this;}
friend MInt operator*(const MInt &a, const MInt &b) {MInt ans=a; ans*=b; return ans;}
friend MInt operator+(const MInt &a, const MInt &b) {MInt ans=a; ans+=b; return ans;}
friend MInt operator-(const MInt &a, const MInt &b) {MInt ans=a; ans-=b; return ans;}
friend MInt operator/(const MInt &a, const MInt &b) {MInt ans=a; ans/=b; return ans;}
friend std::istream &operator>>(std::istream &is, MInt &a) {int u; is>>u; a=MInt(u); return is;}
friend std::ostream &operator<<(std::ostream &os, const MInt &a) {os<<a.val(); return os;}
MInt power(int b) const {int r=1, t=x; while(b){if(b&1) r=r*t%MOD; t=t*t%MOD; b/=2;} return MInt(r);}
};
using mint = MInt<998244353>;
const int N = 1005;
const int M = 5005;
int n, m, k;
mint dp[M], pre[M];
mint sum(int l, int r) {
return l <= r ? pre[r] - pre[l-1] : 0;
}
void solve() {
cin >> n >> m >> k;
rep(j,1,m) dp[j] = 1;
partial_sum(dp+1, dp+1+m, pre+1);
rep(i,2,n) {
rep(j,1,m) {
if (k == 0)
dp[j] = sum(1,m);
else
dp[j] = sum(1,j-k) + sum(j+k,m);
}
partial_sum(dp+1, dp+1+m, pre+1);
}
cout << pre[m].val() << "\n";
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}

posted on   chenfy27  阅读(56)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示