POJ 3254 Corn Fields (状压dp)

网友投稿 284 2022-08-30

POJ 3254 Corn Fields (状压dp)

Description

Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can’t be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

Input

Line 1: Two space-separated integers: M and N Lines 2..M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

Output

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Input

2 31 1 10 1 0

Sample Output

9

题意

在一个 0,1 组成的数字矩阵中选取 0,1,2..k 个 1 ,且所有的 1 都不相邻的方案总数。

思路

​​dp[i][j][k]​​​ 代表第 ​​i​​​ 行的状态为 ​​j​​​ (二进制形式),且总共挑选了 ​​k​​​ 个 ​​1​​​ 满足题意的前 ​​i​​ 行选择方案数

我们确定一个 ​​judge​​​ 方法,其目的是判断一个整数 ​​x​​​ 的二进制中是否含有相邻的 ​​1​​​ ,若有,返回结果 ​​-1​​​ ,否则返回该二进制数中 ​​1​​ 的个数

于是,对于第一行,所有的合法状态的 ​​dp​​​ 取值都为 ​​1​​

对于第 ​​i​​​ 行的某个状态 ​​j​​ ,显然它可以由上一行与当前行所有不相交的方案转移而来

已知第 ​​i-1​​​ 行的最大可行方案为 ​​~j&((1<

状态转移方程:

​​dp[i][j][num+k]+=dp[i-1][s][k]​​

其中, ​​num​​​ 为当前行合法状态中 ​​1​​​ 的个数, ​​k​​​ 为前 ​​i-1​​​ 行挑选的数目, ​​s​​​ 为第 ​​i-1​​ 行一个合法的不相交子集。

AC 代码

#include#includeusing namespace std;const int maxn = 15;const int maxm = 5000+10;const int maxk = 75;const int mod = 100000000;typedef unsigned __int64 LL;LL dp[maxn][maxm][maxk]; //第 i 行状态为 j 且总共挑选 k 个数满足题意的前 i 行选择方案int a[maxn],n,m;int judge(int x){ int ans=0; for(int i=1; i<=x; i<<=1) if(i&x) { ans++; if(i>>1&x)return -1; } return ans;}void solve(){ for(int i=1; i<=n; i++) //枚举行 for(int j=0; j<1<>n>>m) { memset(dp,0,sizeof(dp)); for(int i=1; i<=n; i++) { int x=0; for(int j=0,temp; j>temp; x=(x<<1)+temp; //每行保存为一个二进制数 } a[i]=x; } solve(); LL ans=0; for(int i=0; i<1<

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:跨界营销怎么玩?奥特朗盲盒营销太“硬核!
下一篇:Codeforces 852 G. Bathroom terminal(trie)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~