#include <iostream>
#include <fstream>
#include <memory>
using namespace std;
fstream fin;
int n,k,num[5],rk[5],ans[5][8],ma,mi;
void ready()
{
fin>>n>>k;
for (int i=1;i<=n;i++)
{
fin>>num[i];
if (num[i]>ma) ma=num[i];
rk[i]=(1 << num[i])-1;
}
if (ma>n)
mi=n;
else
mi=ma;
}
bool chk(int rook[])
{
for (int i=1;i<=n;i++)
if (rook[i]!=0)
return false;
return true;
}
void poly(int dep,int lev,int pow)
{
int tmp[5];
if (chk(rk)) //如果检查是空棋盘
{
ans[0][dep]=1;
return;
}
memcpy(tmp,rk,sizeof(tmp));
while (rk[lev]==0) lev++;
while ((rk[lev] & (1 << pow))!=(1 << pow))
pow++;
rk[lev]=rk[lev]- (1 << pow);
if (rk[lev]==0)
poly(dep+1,lev+1,0);
else poly(dep+1,lev,pow+1);
for (int l=0;l<=mi;l++)
{ans[l][dep]+=ans[l][dep+1];
ans[l][dep+1]=0;}
memcpy(rk,tmp,sizeof(rk));
rk[lev]=0;
for (int k=lev+1;k<=n;k++)
if ((rk[k])>=(1 << pow))
rk[k]=rk[k]- (1 << pow);
poly(dep+1,lev+1,0);
for (int l=1;l<=mi;l++)
{ans[l][dep]+=ans[l-1][dep+1];
ans[l-1][dep+1]=0;}
memcpy(rk,tmp,sizeof(rk));
}
int main()
{ fin.open("c:\\test.txt");
ready();
poly(1,1,0);
cout<<ans[k][1];
fin.close();
}
没有评论:
发表评论