Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

状压DP,有不太详细的注释,719ms

Posted by OZY123 at 2016-11-02 20:09:27 on Problem 2411
#include<cstdio>
#include<cstring>
const long long N=12;
long long n,m;
long long f[N][(1<<12)];//第i行的状态为j,i-1行都已经全部填满有多少种状态 
long long tt;
bool ooo[(1<<12)];
//0:没有覆盖  1:已经覆盖 
bool check (long long x,long long y)//x和y是否可以相得益彰 
{
	if ((x|y)!=tt-1) return false;
	long long kk=(x&y);
	return ooo[kk];
}
void prepare ()
{
	memset(ooo,true,sizeof(ooo));
	for (long long u=0;u<(1<<12);u++)
	{
		long long x=u;
		bool last=false;
		while (x>0)
		{
			if ((x&1)==1)
				last=!last;
			else if (last==true) 
			{
				ooo[u]=false;
				break;
			}
			x=x>>1;
		}
		if (last==true) ooo[u]=false;
	}
	return ;
}
int main()
{
	prepare();
	while (true)
	{
		memset(f,0,sizeof(f));
		scanf("%I64d%I64d",&n,&m);
		if (n==0&&m==0) break;
		if (((n*m)&1)==1) {printf("0\n");continue;}
		tt=(1<<m);
		for (long long u=0;u<tt;u++) 
			if (ooo[u]==true) 
				f[1][u]=1;
		for (long long u=2;u<=n;u++)
			for (long long j=0;j<tt;j++)//枚举状态 
				for (long long k=0;k<tt;k++)//枚举上一行的状态
					if (check(j,k)==true)
						f[u][j]+=f[u-1][k];
		printf("%I64d\n",f[n][(tt-1)]);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator