Kaidul

10364

Jul 8th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. static int sticks[1 << 5], side, n;
  8. unsigned dp[1 << 15];
  9.  
  10. int Set(int N, int pos) {
  11.     return N = N | (1 << pos);
  12. }
  13. bool check(int N, int pos){
  14.     return (bool)(N & (1 << pos));
  15. }
  16.  
  17. int square(int s, int i) {
  18.  
  19.     if ( check(dp[i >> 5], i & 31) ) return 0;
  20.  
  21.     dp[i >> 5] = Set(dp[i >> 5], i & 31);
  22.  
  23.     if (s > side) return 0;
  24.  
  25.     if (i == (1 << n) - 1) return 1;
  26.  
  27.     if (s == side) s = 0;
  28.  
  29.     for (int j = 0; j < n; j++)
  30.         if ( ((j >> i) & 1) == 0 && square(s + sticks[j], i | (1 << j)) )
  31.             return 1;
  32.  
  33.     return 0;
  34. }
  35.  
  36. int solve() {
  37.     side = 0;
  38.  
  39.     for (int i = 0; i < n; i++)
  40.         side += sticks[i];
  41.  
  42.     if ((side % 4) != 0) return 0;
  43.  
  44.     side /= 4;
  45.  
  46.     memset(dp, 0, sizeof(dp));
  47.  
  48.     return square(0, 0);
  49. }
  50.  
  51. int main() {
  52. #ifndef ONLINE_JUDGE
  53.     freopen("input.txt", "r", stdin);
  54. #endif
  55.  
  56.     int tcase;
  57.     scanf("%d", &tcase);
  58.     while(tcase--) {
  59.         scanf("%d", &n);
  60.         for (int i = 0; i < n; i++)
  61.             scanf("%d", &sticks[i]);
  62.  
  63.         printf(solve() ? "yes\n" : "no\n");
  64.     }
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment