USACO ORZ
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5581 Accepted Submission(s): 1852
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.
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)
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
Recommend
---------------------------------------------------------------------------------------------------------------------------------

1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<iostream> 5 #define maxn 2333333 6 #define MODD 903457 7 #define ull unsigned long long 8 using namespace std; 9 struct node{ 10 int next; 11 ull to; 12 }; 13 node e[maxn]; 14 int read(); 15 int T,N,l[23],pre[MODD+7],cnt,sum[23],K=1000000007; 16 void dfs(int,int,int); 17 bool cheak(ull); 18 void add(ull); 19 int main(){ 20 T=read();cnt=0; 21 if(T==0){ 22 printf("0"); 23 return 0; 24 } 25 for(int i=1;i<=T;i++){ 26 N=read(); 27 memset(pre,0,sizeof(pre)); 28 memset(sum,0,sizeof(sum)); 29 memset(e,0,(cnt+3)*sizeof(node));cnt=0; 30 for(int j=1;j<=N;j++) l[j]=read(),sum[j]=sum[j-1]+l[j]; 31 dfs(0,0,0); 32 printf("%d ",cnt); 33 } 34 return 0; 35 } 36 void dfs(int a,int b,int xb){ 37 if(xb>N) return; 38 int c=sum[N]-a-b; 39 if(a<=b&&b<=c&&a+b>c){ 40 ull val=a; 41 val=val*K+b; 42 val=val*K+c; 43 if(!cheak(val)) add(val); 44 } 45 dfs(a+l[xb+1],b,xb+1); 46 dfs(a,b+l[xb+1],xb+1); 47 dfs(a,b,xb+1); 48 } 49 bool cheak(ull val){ 50 int i=pre[val%MODD]; 51 while(i&&e[i].to!=val) i=e[i].next; 52 return i>0; 53 } 54 void add(ull val){ 55 int a=val%MODD; 56 e[++cnt].to=val;e[cnt].next=pre[a];pre[a]=cnt; 57 } 58 int read(){ 59 int ans=0,f=1;char c=getchar(); 60 while('0'>c||c>'9'){if(c=='-')f=-1;c=getchar();} 61 while('0'<=c&&c<='9')ans=ans*10+c-48,c=getchar();return ans*f; 62 }