给定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)
。
#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;
}
标签:const,int,sum,abc253E,之差,序列,MInt,rep,dp
From: https://www.cnblogs.com/chenfy27/p/18081367