题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2115
Equal
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 59(26 users) Total Accepted: 26(20 users) Rating: Special Judge: No
Description
Given a set of positive numbers, can you find that if these numbers can be separated into 6 subsets, so that the sum of each subsets is equal.
For example, a set of {1, 1, 1, 1, 1, 1} can meet the condition, but {1, 2, 3, 4, 5, 6} can not.
Input
The input contains multiple test cases.
The first line contains a integer T, means the number of test cases.
Each test case begins with a integer N(N<=30) the number of numbers in the set, followed by N integers, each number is between 1 and 10000.
Output
For each test case print “yes” if the set full-fill the problem, and “no” if not.
Sample Input
2
20 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 5 4 3 2 1
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output
yes
no
Source
ACM-ICPC黑龙江省第九届大学生程序设计竞赛选拔赛(2)
【中文题意】给你n个数,问你能不能用这n个数组成6个相等的数(每个数是其它数的和),并且这n个数每个数只能用一次。
【AC代码】
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
#define LL long long
int a[35],n;
int book[35];
int dfs(int num,int j)
{
for(int i=j;i<=n;i++)
{
if(i==n&&num!=a[i])
{
return 0;
}
if(book[i]==0&&num>=a[i])
{
book[i]=1;
if(num-a[i]==0)
{
return 1;
}
else if(dfs(num-a[i],i+1)==1)
{
return 1;
}
else
{
book[i]=0;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int maxn=0,sum=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
maxn=max(maxn,a[i]);
sum+=a[i];
book[i]=0;
}
if(sum%6!=0||maxn>sum/6)
{
printf("no\n");
}
else
{
//printf("++%d\n",sum);
int ss=0;
for(int i=1;i<=6;i++)
{
if(dfs(sum/6,1))ss++;
/*printf("***\n");
for(int i=1;i<=n;i++)
{
printf("%d ",book[i]);
}
printf("\n");*/
}
//printf("%d\n",ss);
if(ss==6)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
}
return 0;
}