-
Notifications
You must be signed in to change notification settings - Fork 2
/
UVA 10364 - Square.cpp
48 lines (40 loc) · 963 Bytes
/
UVA 10364 - Square.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
int n, in[25], memo[4][1<<20], len;
bool dp(int num, int u, int sum)
{
if(u == (1<<n) - 1 && num == 4) return 1;
int &ret = memo[num][u];
if(ret != -1) return ret;
ret = 0;
for(int i=0; i<n; i++){
if( !((1<<i) & u) ){
if(sum + in[i] < len){
if(dp(num, u | (1<<i), sum + in[i])) return 1;
}else if(sum + in[i] == len)
if(dp(num + 1, u | (1<<i), 0) ) return 1;
}
}
return ret;
}
int main()
{
int tc;
scanf("%d", &tc);
while(tc--){
scanf("%d", &n);
int sum = 0;
for(int i=0; i<n; i++){
scanf("%d", &in[i]);
sum += in[i];
}
if(sum % 4){
printf("no\n");
continue;
}
len = sum / 4;
memset(memo, -1, sizeof memo);
if(dp(0, 0, 0)) printf("yes\n");
else printf("no\n");
}
}