状压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);
}
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
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
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
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]