当前位置:网站首页>状态压缩DP AcWing 291. 蒙德里安的梦想
状态压缩DP AcWing 291. 蒙德里安的梦想
2022-07-03 08:41:00 【T_Y_F666】
状态压缩DP AcWing 291. 蒙德里安的梦想
原题链接
算法标签
动态规划 状态压缩DP
思路

具体思路


代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 12, M = 1 << N;
int f[N][M], st[M];
vector<int> sta[M];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
while(n=read(), m=read(), n||m){
//预处理:判断合并列的状态是否合法
//如果合并列的某行是1表示横放,是0表示竖放
//如果合并列不存在连续的奇数个0,即为台法状态
rep(i, 0, 1<<n){
// cnt 为当前已经存在多少个连续的0
int cnt=0;
st[i]=true;
rep(j, 0, n){
if(i>>j&1){// 如果是1
if(cnt&1){// 如果连续0的个数奇数
st[i]=false;// 记录i不合法
cnt=0;
}
}
else{// 如果是1, 记录0的个数
cnt++;
}
}
// 处理高位0的个数 以4为例 0100 也是非法数字
if(cnt&1){
st[i]=false;
}
}
//状态计算
memset(f, 0, sizeof f);
// 第0列不横放是一种合法的方案
f[0][0]=1;
rep(i, 1, m+1){//阶段: 枚举列
rep(j, 0, 1<<n){//状态:枚举第i列的状态
rep(k, 0, 1<<n){//状态:枚举第11列的状态
// j & k == 0 表示 i 列和 i - 1列同一行不同时捅出来
// st[j | k] == 1 表示 在 i 列状态 j, i - 1 列状态 k 的情况下是合法的.
// 两列状态兼容:不出现重叠的1,不出现连续的奇数个日
if((j&k)==0&&st[j|k]){
f[i][j]+=f[i-1][k];
}
}
}
}
//第m列不横放, 即答案
printf("%lld\n", f[m][0]);
}
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Unity Editor Extension - event handling
- [RPC] RPC remote procedure call
- Notes and bugs generated during the use of h:i:s and y-m-d
- 求组合数 AcWing 886. 求组合数 II
- 【Rust笔记】06-包和模块
- Phpstudy 80 port occupied W10 system
- Log4j2 vulnerability recurrence and analysis
- Binary to decimal, decimal to binary
- 单调栈-84. 柱状图中最大的矩形
- How does unity fixedupdate call at a fixed frame rate
猜你喜欢

记忆化搜索 AcWing 901. 滑雪

求组合数 AcWing 886. 求组合数 II

Constraintlayout's constraintset dynamically modifies constraints
![[concurrent programming] Table hopping and blocking queue](/img/b7/023991a00956e469af855e7a81e126.jpg)
[concurrent programming] Table hopping and blocking queue

Redux - learning notes

Deep parsing JVM memory model

Alibaba canaladmin deployment and canal cluster Ha Construction

Try to reprint an article about CSDN reprint

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

Phpstudy 80 port occupied W10 system
随机推荐
VIM learning notes from introduction to silk skating
Notes on understanding applets 2022/7/3
Life cycle of Servlet
Collection interface
树形DP AcWing 285. 没有上司的舞会
First Servlet
22-05-26 Xi'an interview question (01) preparation
Gif remove blank frame frame number adjustment
[redis] redis persistent RDB vs AOF (source code)
How to delete CSDN after sending a wrong blog? How to operate quickly
Really explain the five data structures of redis
DOM 渲染系统(render mount patch)响应式系统
Redis cluster series 4
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Redux - learning notes
Graphics_ Games101/202 learning notes
Dom4j遍历和更新XML
Dealing with duplicate data in Excel with xlwings
[rust notes] 12 closure
Phpstudy 80 port occupied W10 system