当前位置:网站首页>状态压缩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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- On the difference and connection between find and select in TP5 framework
- [rust notes] 08 enumeration and mode
- Phpstudy 80 port occupied W10 system
- Slice and index of array with data type
- 数据库原理期末复习
- How to delete CSDN after sending a wrong blog? How to operate quickly
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
- 如何应对数仓资源不足导致的核心任务延迟
- Constraintlayout's constraintset dynamically modifies constraints
- Unity editor expansion - the framework and context of unity imgui
猜你喜欢

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

Deeply understand the underlying data structure of MySQL index

DOM 渲染系统(render mount patch)响应式系统

Alibaba canaladmin deployment and canal cluster Ha Construction

Simple demo of solving BP neural network by gradient descent method

Markdown learning

Deep parsing JVM memory model

Drawing maze EasyX library with recursive backtracking method

20220630学习打卡

Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
随机推荐
Dealing with duplicate data in Excel with xlwings
Explain sizeof, strlen, pointer, array and other combination questions in detail
Drawing maze EasyX library with recursive backtracking method
Markdown directory generation
分配异常的servlet
如何应对数仓资源不足导致的核心任务延迟
[concurrent programming] thread foundation and sharing between threads
Binary to decimal, decimal to binary
Parameters of convolutional neural network
【Rust 笔记】10-操作符重载
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
Location of package cache downloaded by unity packagemanager
UE4 source code reading_ Mobile synchronization
URL backup 1
【Rust 笔记】13-迭代器(上)
【Rust 笔记】07-结构体
[redis] redis persistent RDB vs AOF (source code)
Markdown learning
Pit & ADB wireless debugging of vivo real machine debugging
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time