HDU - 4277


USACO ORZ



Submit Status




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






132 3 4





 






Sample Output






1





 




Source



2012 ACM/ICPC Asia Regional Changchun Online



//题意:输入一个数n,再输入n个数,表示n根木棒的长度



表示给你n根长短不一的木棒,现在让你将这n根木棒都用上,问可以组成几个不相同的三角形(不相同表示至少有一条边的长度不同)。



//思路:



运用dfs将所有的能组成的三角形都找出来,再用set判重,最后得到的即为所求。




#include<stdio.h>
#include<string.h>
#include<set>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ll long long
#define N 20
using namespace std;
int A[N];
int SUM;
set<ll>s;
set<ll>::iterator f;
ll ans,n;
void dfs(ll a,ll b,ll c,ll cnt,ll sum)
{
	if(cnt>=n)//所有的边都被加上 
	{
		if(a>b) swap(a,b);
		if(a>c) swap(a,c);
		if(b>c) swap(b,c);
		if(a+b>c)//如果能组成三角形 
		{
			ll h=(ll)a*SUM*SUM+b*SUM+c;//乘上SUM是为了放大边的长度的不同 
			if(s.find(h)==s.end())
			{
				s.insert(h);
				ans++;
			} 
		}
		return ;
	}
	sum-=A[cnt];
	if(a+A[cnt]<b+c+sum&&a+A[cnt]<=b+sum) dfs(a+A[cnt],b,c,cnt+1,sum);//先判断在能组成三角形的条件下再dfs 
	if(b+A[cnt]<a+c+sum&&b+A[cnt]<=c+sum) dfs(a,b+A[cnt],c,cnt+1,sum);
	if(c+A[cnt]<a+b+sum) dfs(a,b,c+A[cnt],cnt+1,sum);
}
int main()
{
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		s.clear();
		scanf("%d",&n);
		SUM=0;
		for(i=0;i<n;i++)
			scanf("%d",&A[i]),SUM+=A[i];
		ans=0;
		dfs(0,0,0,0,SUM);
		printf("%lld\n",ans);
	}
	return 0;
}





Time Limit: 1500MS

 

Memory Limit: 32768KB

 

64bit IO Format: %I64d & %I64u