跳到正文

剪贴板

剪贴板 1hhf3gex

原文
剪贴板信息

剪贴板信息

收录
更新

P1896 [SCOI2005] 互不侵犯

C++
#include<bits/stdc++.h>
using namespace std;
int n,k;
long long f[11][100][1<<9];
bool check(int s)
{
	if(s&(s>>1))return false;
	if(s&(s<<1))return false;
	return true;
}
bool check(int s,int t)
{
	if(s&t)return false;
	if(s&(t>>1))return false;
	if((s>>1)&t)return false;
	if((s<<1)&t)return false;
	if(s&(t<<1))return false;
	return true;
}
int cal(int s)
{
	int res=0;
	while(s)
	{
		if(s&1)res++;
		s>>=1;
	}
	return res;
}
int main()
{
	cin>>n>>k;

	f[0][0][0]=1;

		for(int i=1;i<=n+1;i++)
			for(int s=0;s<(1<<n);s++)
				if(check(s))
				for(int t=0;t<(1<<n);t++)
				if(check(t)&&check(s,t))
					for(int j=0;j<=k;j++)
						if(j>=cal(t))
						f[i][j][t]+=f[i-1][j-cal(t)][s];

	cout<<f[n+1][k][0];
	return 0;
}