当前位置:网站首页>状态压缩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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 【Rust笔记】06-包和模块
- Development material set
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
- Constraintlayout's constraintset dynamically modifies constraints
- PHP uses foreach to get a value in a two-dimensional associative array (with instances)
- Concurrent programming (III) detailed explanation of synchronized keyword
- Unity multi open script
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- 796 · unlock
- [concurrent programming] working mechanism and type of thread pool
猜你喜欢

单调栈-42. 接雨水

MySQL three logs

Allocation exception Servlet

Visual Studio (VS) shortcut keys
![[concurrent programming] Table hopping and blocking queue](/img/b7/023991a00956e469af855e7a81e126.jpg)
[concurrent programming] Table hopping and blocking queue

Es8 async and await learning notes

Servlet的生命周期

数据库原理期末复习

Facial expression recognition based on pytorch convolution -- graduation project

JS non Boolean operation - learning notes
随机推荐
TP5 multi condition sorting
How to delete CSDN after sending a wrong blog? How to operate quickly
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
【Rust笔记】05-错误处理
Monotonic stack -503 Next bigger Element II
Final review of Database Principles
C language student management system based on linked list, super detailed
Servlet的生命周期
[concurrent programming] explicit lock and AQS
树形DP AcWing 285. 没有上司的舞会
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
Monotonic stack -84 The largest rectangle in the histogram
[rust notes] 08 enumeration and mode
Solution of 300ms delay of mobile phone
LinkedList set
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Eating fruit
[rust notes] 05 error handling
单调栈-84. 柱状图中最大的矩形
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial