当前位置:网站首页>State compression DP acwing 291 Mondrian's dream
State compression DP acwing 291 Mondrian's dream
2022-07-03 09:06:00 【T_ Y_ F666】
State compression DP AcWing 291. Mondrian's dream
Original link
Algorithm tags
Dynamic programming State compression DP
Ideas

Specific ideas


Code
#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){
// Preprocessing : Judge whether the status of the consolidated column is legal
// If a row of the merged column is 1 It means horizontal , yes 0 Indicates vertical placement
// If there is no continuous odd number of merged columns 0, It is the state of Taiwan France
rep(i, 0, 1<<n){
// cnt For how many consecutive 0
int cnt=0;
st[i]=true;
rep(j, 0, n){
if(i>>j&1){// If it is 1
if(cnt&1){// If it's continuous 0 The number of is odd
st[i]=false;// Record i illegal
cnt=0;
}
}
else{// If it is 1, Record 0 The number of
cnt++;
}
}
// Handle high order 0 The number of With 4 For example 0100 It is also an illegal number
if(cnt&1){
st[i]=false;
}
}
// State calculation
memset(f, 0, sizeof f);
// The first 0 It is a legal scheme that columns are not placed horizontally
f[0][0]=1;
rep(i, 1, m+1){// Stage : Enumerating columns
rep(j, 0, 1<<n){// state : Enumerate the i The state of the column
rep(k, 0, 1<<n){// state : Enumerate the 11 The state of the column
// j & k == 0 Express i Column sum i - 1 List the same row and poke it out at the same time
// st[j | k] == 1 Express stay i Column status j, i - 1 Column status k Is legal .
// The two columns are compatible : There is no overlap 1, There are no consecutive odd days
if((j&k)==0&&st[j|k]){
f[i][j]+=f[i-1][k];
}
}
}
}
// The first m The columns are not placed horizontally , That is the answer.
printf("%lld\n", f[m][0]);
}
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- Using variables in sed command
- Query XML documents with XPath
- 剑指 Offer II 029. 排序的循环链表
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- What is an excellent fast development framework like?
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
- Solution of 300ms delay of mobile phone
- Binary tree sorting (C language, int type)
- createjs easeljs
- How to use Jupiter notebook
猜你喜欢

Really explain the five data structures of redis

求组合数 AcWing 885. 求组合数 I

Notes and bugs generated during the use of h:i:s and y-m-d

LeetCode 1089. Duplicate zero

AcWing 786. Number k

LeetCode 241. 为运算表达式设计优先级

Character pyramid

Six dimensional space (C language)

Debug debugging - Visual Studio 2022

LeetCode 57. 插入区间
随机推荐
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
AcWing 787. Merge sort (template)
LeetCode 57. Insert interval
In the digital transformation, what problems will occur in enterprise equipment management? Jnpf may be the "optimal solution"
Find the combination number acwing 886 Find the combination number II
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
Discussion on enterprise informatization construction
LeetCode 438. Find all letter ectopic words in the string
Phpstudy 80 port occupied W10 system
低代码起势,这款信息管理系统开发神器,你值得拥有!
The difference between if -n and -z in shell
Debug debugging - Visual Studio 2022
网络安全必会的基础知识
Concurrent programming (III) detailed explanation of synchronized keyword
Binary tree sorting (C language, int type)
Allocation exception Servlet
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
Sword finger offer II 091 Paint the house
Baidu editor ueeditor changes style
AcWing 787. 归并排序(模板)