G - 状压dp

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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 3
1 1 1
0 1 0

Sample Output

9

Hint

Number the squares as follows:

1 2 3
  4  

There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

解析见代码:
tips:位运算优先级极其诡异,在使用位运算的时候最好要加上括号
总结下本题用到的几种位运算:
1.判断一个数的二进制位是否有两个相邻的1 x&(x<<1)==0
2.取一个数的第k位 p&(1<<(k-1))
3.判断两个数是不是同一位有1 a&b==0?
/*
poj3254
分类:状压DP
因为后一行的状态只与前一行有关,考虑使用DP做
每一行最多有12个格子,用0表示不取,1表示取总共有1<<12-1种情况
首先单独看某一行,合法的状态是没有两个1彼此相邻,用位运算来判断
i&(i<<1)为0则表示没有相邻的1,是合法状态。
先初始化出所有的不考虑某块地不能选择的情况,保存所有的合法状态,
然后再结合题目进行筛选,筛选范围时1~(1<<m),看那种合法状态是否
与草原实际相符,具体办法就是依次取合法状态的某一位,如果改位为
1但实际上这块草地不能取,这种状态就是不可行的,第一行若第j种状态可行
dp[i][j]=1,状态转移方程则是dp[i][j]+=dp[i-1][k],k代表上一层的可行状态
若不可行,dp[i-1][k]=0,所以初始化时要将dp数组初始化为0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=(<<);//最多有12列
int v[maxn],dp[][maxn];
int a[][];//用来存图;
int n,m;
const long long mod=;
int tot;
void init()
{
tot=;
for(int i=;i<maxn;i++)//记录所有合法状态
{
if((i&(i<<))==)
v[tot++]=i;
}
}
bool check(int row,int p)
{
for(int i=;i<=m;i++)
{
if((p&(<<(i-)))&&!a[row][i])//这种状态不符合实际
return true;
}
return false;
}
int main()
{
init();
// for(int i=0;i<tot;i++)
// cout<<v[i]<<" ";
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++)
{
for(int j=;v[j]<(<<m);j++)
{
if(check(i,v[j])) continue;
if(i==)
{
dp[i][j]=;
continue;
}
for(int k=;v[k]<(<<m);k++)
{
if((v[j]&v[k])==)
dp[i][j]+=dp[i-][k];
}
}
}
__int64 ans=;
for(int i=;v[i]<(<<m);i++)
{
ans=(ans+dp[n][i])%mod;
}
printf("%I64d\n",ans);
}
}

poj3254状压DP入门的更多相关文章

  1. 状压dp入门

    状压dp的含义 在我们解决动态规划题目的时候,dp数组最重要的一维就是保存状态信息,但是有些题目它的具有dp的特性,并且状态较多,如果直接保存的可能需要三维甚至多维数组,这样在题目允许的内存下势必是开 ...

  2. poj2686 状压dp入门

    状压dp第一题:很多东西没看懂,慢慢来,状压dp主要运用了位运算,二进制处理 集合{0,1,2,3,....,n-1}的子集可以用下面的方法编码成整数 像这样,一些集合运算就可以用如下的方法来操作: ...

  3. 状压DP入门详解+题目推荐

    在动态规划的题型中,一般叫什么DP就是怎么DP,状压DP也不例外 所谓状态压缩,一般是通过用01串表示状态,充分利用二进制数的特性,简化计算难度.举个例子,在棋盘上摆放棋子的题目中,我们可以用1表示当 ...

  4. POJ:1185-炮兵阵地(状压dp入门)

    炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组 ...

  5. POJ3254 状压dp

                                                                                                    Corn ...

  6. POJ 3254 & POJ 1185(状压DP入门)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16773   Accepted: 8860 Desc ...

  7. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  8. 状压dp入门第一题 poj3254

    题目链接 http://poj.org/problem?id=3254 转自http://blog.csdn.net/harrypoirot/article/details/23163485 #inc ...

  9. poj3254(状压dp入门第一道题,很详细)

    题目链接:http://poj.org/problem?id=3254 学习博客:https://blog.csdn.net/harrypoirot/article/details/23163485 ...

随机推荐

  1. [转] JS运算符 &&和|| 及其优先级

    第一.&& (逻辑与)运算,看一个简单的例子: var a = 1 && 2 && 3; var b = 0 && 1 &&am ...

  2. 在线编辑器kindEditor

    操作文档:http://kindeditor.net/doc.php 文件下载

  3. Css3从IE6-IE9的支持查看

    http://msdn.microsoft.com/en-us/library/cc351024%28v=vs.85%29.aspx http://caniuse.com/

  4. 将EmEditor加入到鼠标右键菜单

    在清理系统的时候,无意中将EmEditor的鼠标右键功能给清理掉了,在EmEditor的配置中又没有找到如何加入到鼠标右键菜单的方法,只好使用导入注册表功能了,以下的代码,拷贝到记事本中,保存为EmE ...

  5. 如何用PowerPoint制作闪烁的星星

    在PPT中,PPT动画说是幻灯片PPT中的精华是当之无愧的!ppt文件有了动画,犹如插上翅膀的鸟,让PPT的色彩衍生出了更多的特色.只要你的ppt动画效果制作的对,你的幻灯片将明显与众不同,观众也更容 ...

  6. python实现tailf

    # -*- coding:utf-8 -*- ''' Created on 2016年10月28日 @author: zhangsongbin ''' import time class file_r ...

  7. Big Data Security Part One: Introducing PacketPig

    Series Introduction Packetloop CTO Michael Baker (@cloudjunky) made a big splash when he presented ‘ ...

  8. wifi10db定向天线制作及应注意的问题

    2.4GHZ本身就是高频要求制作精度高,如果您动手能力差的话还是不要做的好许多网友看到网上的制作资料就急不可耐的去找材料,然后加班加点的制作.等做出了天线发现效果不怎么样,或出了这样和那样的问题,才肯 ...

  9. mysql之事务

    事务处理 begin        开始一个事物 commit        事务确认 rollback    事务回滚 end        事务结束 innodb下可以实现事务 开始执行事务时如果 ...

  10. js中iframe的用法

    最近遇到的项目总是习惯左边一个树,点击每个树的节点右边出现相应的信息,遇到这种情况用iframe还是很简单的, 例如 : 页面文件 @section Tree{ <ul id="tre ...