您的位置:首页 > 编程语言 > C语言/C++

HNIO 2013 bzoj 3139 比赛

2016-02-21 16:51 459 查看
爆搜加减枝,取模真是可怕800ms 和 tle 的区别

/**************************************************************
Problem: 3139
User: Clare
Language: C++
Result: Accepted
Time:852 ms
Memory:2336 kb
****************************************************************/

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define N 11
#define Mod 1000000007

int n;
int A
,tmp
;
map<long long,long long> Ans;

int Cmp(const int &a,const int &b)
{
return a>b;
}

long long Hash(int len)
{
for(int i=1;i<=len;i++)
tmp[i]=A[i];
sort(tmp+1,tmp+len+1,Cmp);
long long Sum=len;
for(int i=1;i<=len;i++)
Sum=Sum*28+tmp[i];
return Sum;
}

long long Search(int pos,int len)
{
if(A[len]>3*(len-pos))
return -1;
long long Sum=0;
if(pos==len)
{
if(len==1)
return 1;
else
{
long long qwer=Hash(len-1);
if(Ans[qwer])
return Ans[qwer];
else return Ans[qwer]=Search(1,len-1);
}
}
if(A[len]>=3)
{
A[len]-=3;
long long qwer=Search(pos+1,len);
if(qwer!=-1)
Sum=(Sum+qwer)%Mod;
A[len]+=3;
}
if(A[pos]>=3)
{
A[pos]-=3;
long long qwer=Search(pos+1,len);
if(qwer!=-1)
Sum=(Sum+qwer)%Mod;
A[pos]+=3;
}
if(A[len]>0&&A[pos]>0)
{
A[len]--;A[pos]--;
long long qwer=Search(pos+1,len);
if(qwer!=-1)
Sum=(Sum+qwer)%Mod;
A[len]++;A[pos]++;
}
return Sum?Sum:-1;
}

int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&A[i]);
sort(A+1,A+n+1,Cmp);
printf("%lld\n",Search(1,n));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言