昨天刚做了一个搜索题,感觉和这个的剪枝方法很像。
当把剩下的数都选上时依然满足条件,则用组合数优化。
sta[i] 标记的为 从第 i 个到 第 n 张牌一共出现多少个数,最多五十个数,状压就好了。
STL 真心不会调试。。
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-6)
#define LL long long
#define ULL unsigned long long int
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 1000000007
using namespace std;
int fn[51],sn[51];
LL sta[51];
LL ANS;
LL cal[51][51];
void Cal(int n)
{
int i,j;
cal[0][0] = 0;
for(i = 1;i <= n; ++i)
{
for(j = 0;j <= i; ++j)
{
if(j == 1)
cal[j][i] = i;
else if(j == 0 || i == j)
cal[j][i] = 1;
else
cal[j][i] = cal[j-1][i-1] + cal[j][i-1];
}
}
}
void dfs(int s,int len,int k,int ans,LL st)
{
cout<<"s = "<<s<<" len = "<<len<<" k = "<<k<<" ans = "<<ans<<endl;
if(ans > k)
return ;
if(s == len)
{
ANS++;
return ;
}
int i,temp = 0;
for(i = 1;i <= max(10,len); ++i)
{
if((st&(((LL)1)<<i)) == 0 && (sta[s]&(((LL)1)<<i)))
temp++;
}
if(temp+ans <= k)
{
for(i = len-s;i >= 0; --i)
ANS += cal[i][len-s];
return ;
}
dfs(s+1,len,k,ans,st);
if((st&(((LL)1) << fn[s])) == 0)
{
st += (((LL)1) << fn[s]);
ans++;
}
if((st&(((LL)1) << sn[s])) == 0)
{
st += (((LL)1) << sn[s]);
ans++;
}
dfs(s+1,len,k,ans,st);
}
class TaroCards
{
public :
long long int getNumber(vector<int> f,vector<int> s,int k)
{
int len,i;
for(len = f.size(),i = 0; i < len; ++i)
{
fn[i] = f[i];
sn[i] = s[i];
}
sta[len-1] = (((LL)1) << fn[len-1]);
if(fn[len-1] != sn[len-1])
{
sta[len-1] += (((LL)1) << sn[len-1]);
}
for(i = len-2; i >= 0; --i)
{
sta[i] = sta[i+1];
if((sta[i]&(((LL)1) << fn[i])) == 0)
{
sta[i] += (((LL)1) << fn[i]);
}
if((sta[i]&(((LL)1) << sn[i])) == 0)
{
sta[i] += (((LL)1) << sn[i]);
}
}
ANS = 0;
Cal(len);
dfs(0,len,k,0,0);
return ANS;
}
friend void dfs(int s,int len,int k,int ans,LL st);
friend void Cal(int n);
};