- 很难想到的动态规划,优化空间的思路非常巧妙
- 用相对位置来转移
- f[i][j]表示i之前,放置数字的压缩情况为j,的所有方案数
- ** f[i+1][(j | (1 << k)) >> 1] += f[i][j] **
- k表示i放的数字的相对位置
- 具体转移还是看代码
#include<bits/stdc++.h>
#define debug1(a) cout<<#a<<'='<< a << endl;
#define debug2(a,b) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<endl;
#define debug3(a,b,c) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<endl;
#define debug4(a,b,c,d) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<endl;
#define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<" "<<#e<<" = "<<e<<endl;
#define debug0(x) cout << "debug0: " << x << endl
#define fr(t, i, n)for (long long i = t; i < n; i++)
#define YES cout<<"Yes"<<endl
#define nO cout<<"no"<<endl
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int N = 510,mod = 998244353;
int f[N][1<<15];
int a[N];
void solve()
{
f[0][0] = 1;
int n,d;cin >> n >> d;
for(int i = 0;i < n;i ++)
{
cin >> a[i];
a[i] --;
}
for(int i = 0;i < n;i ++)
{
for(int j = 0;j < 1<<(2*d + 1);j ++)
{
if(a[i] >= 0)
{
int temp = a[i] - i + d;
if(~j>>temp&1)(f[i+1][(j | (1<<temp)) >> 1] += f[i][j]) %= mod;
continue;
}
for(int k = 0;k < 2*d+1;k ++)
{
int newi = k - d + i;
if(newi < 0 || newi >= n)continue;
if(j>>k&1)continue;
(f[i+1][(j|(1<<k)) >> 1] += f[i][j]) %= mod;
}
}
}
cout << f[n][((1<<d) - 1)] << endl;
}
signed main()
{
int T = 1;
while(T--){
solve();
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析