hdu4277 USACO ORZ-hash 长春网络赛

hdu4277 USACO ORZ-----hash 长春网络赛

USACO ORZ

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 264    Accepted Submission(s): 94


Problem Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N fence segments and must arrange them into a triangular pasture. Ms. Hei must use all the rails to create three sides of non-zero length. Calculating the number of different kinds of pastures, she can build that enclosed with all fence segments.
Two pastures look different if at least one side of both pastures has different lengths, and each pasture should not be degeneration.

Input
The first line is an integer T(T<=15) indicating the number of test cases.
The first line of each test case contains an integer N. (1 <= N <= 15)
The next line contains N integers li indicating the length of each fence segment. (1 <= li <= 10000)

Output
For each test case, output one integer indicating the number of different pastures.

Sample Input
1 3 2 3 4

Sample Output
1

Source
2012 ACM/ICPC Asia Regional Changchun Online

Recommend
liuyiding
弱啊,水题都做不出。
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
#include<memory.h>
using namespace std;
#define mm 1000007
int s[55],n;
struct hashtable
{
    int size,ans[mm][3],next[mm],h[mm];
    int hash(int a,int b,int c)
    {
      return ((((a*131+b)*131+c)*131)&0x7FFFFFFF)%mm;
    }

     void insert(int a,int b,int c)
    {
        int id=hash(a,b,c);
        for(int i=h[id];i>=0;i=next[i])
        if(ans[i][0]==a&&ans[i][1]==b&&ans[i][2]==c) return;
        ans[size][0]=a,ans[size][1]=b,ans[size][2]=c;
        next[size]=h[id],h[id]=size++;

    }
    void clear()
    {
        memset(h,-1,sizeof(h));
        size=0;
    }
}g;

int ai,bi,ci;
void check(int a,int b,int c)
{
    if(a+b<=c||a+c<=b||b+c<=a) return ;
    g.insert(a,b,c);
}
void dfs(int id)
{
    if(id>=n)
    {
        if(ai<=bi&&bi<=ci) check(ai,bi,ci);
        return ;
    }
    ai+=s[id];
    dfs(id+1);
    ai-=s[id];
    bi+=s[id];
    dfs(id+1);
    bi-=s[id];
    ci+=s[id];
    dfs(id+1);
    ci-=s[id];

}
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
       g.clear();
       scanf("%d",&n);
       for(int i=0;i<n;i++)
       scanf("%d",&s[i]);
       ai=bi=ci=0;
       dfs(0);
       printf("%d\n",g.size);
   }
}