当前位置:网站首页>状态压缩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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Gif remove blank frame frame number adjustment
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- Drawing maze EasyX library with recursive backtracking method
- Query XML documents with XPath
- 高斯消元 AcWing 883. 高斯消元解线性方程组
- [rust notes] 12 closure
- Monotonic stack -503 Next bigger Element II
- Solution of 300ms delay of mobile phone
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
- UE4 source code reading_ Bone model and animation system_ Animation compression
猜你喜欢

Binary tree sorting (C language, int type)

Dom4j遍历和更新XML

JS ternary operator - learning notes (with cases)

22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism

OpenGL learning notes

【Rust笔记】02-所有权

Slice and index of array with data type

too many open files解决方案

单调栈-42. 接雨水

Query XML documents with XPath
随机推荐
单调栈-42. 接雨水
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
[redis] redis persistent RDB vs AOF (source code)
Deep parsing (picture and text) JVM garbage collector (II)
XPath实现XML文档的查询
C language student management system based on linked list, super detailed
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
[rust notes] 07 structure
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
[RPC] RPC remote procedure call
Constraintlayout's constraintset dynamically modifies constraints
22-05-26 Xi'an interview question (01) preparation
Cesium for unreal quick start - simple scenario configuration
记忆化搜索 AcWing 901. 滑雪
Servlet的生命周期
Get the link behind? Parameter value after question mark
Unity notes 1
[concurrent programming] Table hopping and blocking queue
Unity Editor Extension - Outline
How to delete CSDN after sending a wrong blog? How to operate quickly