Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- using namespace std;
- static int sticks[1 << 5], side, n;
- unsigned dp[1 << 15];
- int Set(int N, int pos) {
- return N = N | (1 << pos);
- }
- bool check(int N, int pos){
- return (bool)(N & (1 << pos));
- }
- int square(int s, int i) {
- if ( check(dp[i >> 5], i & 31) ) return 0;
- dp[i >> 5] = Set(dp[i >> 5], i & 31);
- if (s > side) return 0;
- if (i == (1 << n) - 1) return 1;
- if (s == side) s = 0;
- for (int j = 0; j < n; j++)
- if ( ((j >> i) & 1) == 0 && square(s + sticks[j], i | (1 << j)) )
- return 1;
- return 0;
- }
- int solve() {
- side = 0;
- for (int i = 0; i < n; i++)
- side += sticks[i];
- if ((side % 4) != 0) return 0;
- side /= 4;
- memset(dp, 0, sizeof(dp));
- return square(0, 0);
- }
- int main() {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- #endif
- int tcase;
- scanf("%d", &tcase);
- while(tcase--) {
- scanf("%d", &n);
- for (int i = 0; i < n; i++)
- scanf("%d", &sticks[i]);
- printf(solve() ? "yes\n" : "no\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment