#include <bits/stdc++.h> #define e using u=ostream;template<class a,class b>u&operator<<(u&o,pair<a,b>&x) using namespace std;e;u&operator<<(u&o,string&s){return o<<s.c_str();}template< class t>auto operator<<(u&o,t&x)->decltype(x.end(),o){o<<'{';int i=2;for(auto y: x)o<<", "+i<<y,i=0;return o<<'}';}e{return o<<'('<<x.first<<", "<<x.second<<')';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(...) #endif #define ff first #define ss second #define ll long long void solve() { int n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int start = 0; while (start < n && v[start] == 0) { start++; } int end = n - 1; while (end >= 0 && v[end] == 0) { end--; } vector<ll> a; for (int i = start; i <= end; i++) { a.push_back(v[i]); } n = a.size(); if(n == 1) { cout << (a[0] == 1 ? "TAK" : "NIE") << "\n"; return; } vector <vector <set <ll> > > dp(n, vector <set <ll> > (3)); dp[0][0].insert(2*a[0]); dp[0][1].insert(2*a[0]-1); dp[0][2].insert(2*a[0]-2); for (int i = 1; i < n-1; i++) { for (int s = 0; s <= 2; s++) { for (int d = 0; d <= 2; d++) { if (s + d > 2) continue; for (auto xlast : dp[i-1][s]) { ll x = 2*a[i] - d - xlast; if (x > 0) { dp[i][s+d].insert(x); } } } } } LOG(dp); for (int s = 0; s <= 2; s++) { for (auto x : dp[n-2][s]) { LOG(s, x); if (x == 2*a[n-1]-(2-s)) { cout << "TAK\n"; return; } } } cout << "NIE\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--){ solve(); } return 0; }
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include <bits/stdc++.h> #define e using u=ostream;template<class a,class b>u&operator<<(u&o,pair<a,b>&x) using namespace std;e;u&operator<<(u&o,string&s){return o<<s.c_str();}template< class t>auto operator<<(u&o,t&x)->decltype(x.end(),o){o<<'{';int i=2;for(auto y: x)o<<", "+i<<y,i=0;return o<<'}';}e{return o<<'('<<x.first<<", "<<x.second<<')';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(...) #endif #define ff first #define ss second #define ll long long void solve() { int n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int start = 0; while (start < n && v[start] == 0) { start++; } int end = n - 1; while (end >= 0 && v[end] == 0) { end--; } vector<ll> a; for (int i = start; i <= end; i++) { a.push_back(v[i]); } n = a.size(); if(n == 1) { cout << (a[0] == 1 ? "TAK" : "NIE") << "\n"; return; } vector <vector <set <ll> > > dp(n, vector <set <ll> > (3)); dp[0][0].insert(2*a[0]); dp[0][1].insert(2*a[0]-1); dp[0][2].insert(2*a[0]-2); for (int i = 1; i < n-1; i++) { for (int s = 0; s <= 2; s++) { for (int d = 0; d <= 2; d++) { if (s + d > 2) continue; for (auto xlast : dp[i-1][s]) { ll x = 2*a[i] - d - xlast; if (x > 0) { dp[i][s+d].insert(x); } } } } } LOG(dp); for (int s = 0; s <= 2; s++) { for (auto x : dp[n-2][s]) { LOG(s, x); if (x == 2*a[n-1]-(2-s)) { cout << "TAK\n"; return; } } } cout << "NIE\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--){ solve(); } return 0; } |