和1011拼木棒差不多,但是直接拿那个程序居然错了,就是在拼新的木棒的时候忘记判断第一个加入其中的小木棒是不是会超出要拼的木棒的长度。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 64;
int t, n, stick[maxn], each, ok, total, num;
bool used[maxn];
void init()
{
int i;
total = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &stick[i]);
total += stick[i];
}
ok = false;
memset(used, 0, sizeof(used));
}
void dfs(int length, int start)
{
int i;
if (ok)
return;
if (length >= total)
{
ok = true;
return;
}
if (length % each == 0)
{
i = start;
while (used[i])
i++;
used[i] = true;
if (length + stick[i] < length / each * each + each)
dfs(length + stick[i], i + 1);
else if (stick[i] + length == length / each * each + each)
dfs(length + stick[i], 0);
used[i] = false;
return;
}
for (i = start; i < n; i++)
{
if (used[i])
continue;
if (stick[i] + length < length / each * each + each)
{
used[i] = true;
dfs(length + stick[i], i + 1);
used[i] = false;
if (ok)
return;
}
else if (stick[i] + length == length / each * each + each)
{
used[i] = true;
dfs(length + stick[i], 0);
used[i] = false;
return;
}
}
}
int main()
{
int i, t, ca;
//freopen("t.txt", "r", stdin);
scanf("%d", &ca);
while (ca--)
{
scanf("%d", &n);
init();
sort(stick, stick + n);
for (i = 0; i < n / 2; i++)
{
t = stick[i];
stick[i] = stick[n - i - 1];
stick[n - i - 1] = t;
}
if (total % 4 == 0)
{
each = total / 4;
num = 4;
dfs(0, 0);
}
else
ok = false;
if (ok)
printf("yes\n");
else
printf("no\n");
}
return 0;
}