http://acm.hdu.edu.cn/showproblem.php?pid=4277
题意:某人需要用所有的栅栏围成一个三角形,计算最多可以围成多少种不同(至少一条边不同)的三角形?
分析:用set构造唯一值判重,注意要用到所有的木棒,构成三角形
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
const int NM=20;
set<__int64>s1;
int a[NM],n,sum;
void DFS(int x,int y,int z,int i)
{
if(i==n)
{
if(z>x || z>y || y>x) //z<=y<=x,剪枝
return;
if(x && y && z && z+y>x){
__int64 t=x*sum*sum+y*sum+z;
s1.insert(t);
}
return;
}
DFS(x+a[i],y,z,i+1);
DFS(x,y+a[i],z,i+1);
DFS(x,y,z+a[i],i+1);
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum=0;
for(i=0;i<n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
s1.clear();
DFS(0,0,0,0);
printf("%d\n",s1.size());
}
return 0;
}
/*
30
4
1 2 3 4
5
1 1 1 1 1
2 [(3,3,4),(2,4,4)]
1 [(1,2,2)]
*/