poj3254(状压DP)

状压DP虽然以前是学过了。。不过也忘得差不多了吧。。主要考察对位运算的灵活运用,然而这是需要积累的。。另外。。状压DP不是很好debug,往往打出表来也是一脸懵逼的。。

判断行与行之间是否有相邻直接与,列与列之间是否有相邻需要x&(x<<1)即可

其他就直接转移。。



#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 100000000
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 10005
#define nm 15
#define pi 3.141592653
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}



int n,m,a[nm],b[NM],tot;
ll ans,d[nm][NM];

int main(){
	n=read();m=read();
	inc(i,0,succ(m)-1)if((i&(i<<1))==0)b[++tot]=i;
	//inc(i,1,tot)printf("%d ",b[i]);putchar('\n');
//	printf("%d\n",tot);
	inc(i,1,n)inc(j,1,m)a[i]=(a[i]<<1)+read();
	inc(k,1,tot)if((a[1]|b[k])==a[1])d[1][k]++;
	inc(i,2,n)inc(j,1,tot)if((a[i]|b[j])==a[i])
		inc(k,1,tot)if(d[i-1][k]&&(b[j]&b[k])==0)(d[i][j]+=d[i-1][k])%=inf;
	inc(j,1,tot)(ans+=d[n][j])%=inf;
	//inc(i,1,n){inc(j,1,tot)printf("%d ",d[i][j]);putchar('\n');}
	return 0*printf("%lld\n",ans);
}




Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17580 Accepted: 9263

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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值