#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) {
return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e;
return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n';
#else
#define debug(...) {}
#endif
constexpr int inf = (int) (2e9 + 13);
vector<vector<int>> dp(3, vector<int>(2));
vector<vector<int>> temp(3, vector<int>(2));
vector<bool> poss(3);
void solve() {
REP(i, 3)
poss[i] = true;
int n;
cin >> n;
vector<int> a(n);
int L = n + 1, R = -1;
REP(i, n) {
cin >> a[i];
if (a[i] != 0) {
L = min(L, i);
R = i;
}
}
FOR(i, L, R) {
if (a[i] == 0) {
cout << "NIE\n";
return;
}
}
if (L == R) {
if (a[L] == 1)
cout << "TAK\n";
else
cout << "NIE\n";
return;
}
dp[0][0] = dp[0][1] = 2 * a[L];
dp[1][0] = dp[1][1] = 2 * a[L] - 1;
dp[2][0] = dp[2][1] = 2 * a[L] - 2;
if (dp[2][0] == 0)
poss[2] = false;
// debug(dp);
// debug(poss);
FOR(i, L + 1, R - 1) {
REP(j, 3) {
temp[j][0] = inf;
temp[j][1] = -inf;
}
a[i] *= 2;
if (poss[0]) {
temp[0][0] = temp[0][1] = a[i] - dp[0][0];
temp[1][0] = temp[1][1] = temp[0][0] - 1;
temp[2][0] = temp[2][1] = temp[0][0] - 2;
}
if (poss[1]) {
temp[1][0] = min(temp[1][0], a[i] - dp[1][1]);
temp[1][1] = max(temp[1][1], a[i] - dp[1][0]);
temp[2][0] = min(temp[2][0], a[i] - dp[1][1] - 1);
temp[2][1] = max(temp[2][1], a[i] - dp[1][0] - 1);
}
if (poss[2]) {
temp[2][0] = min(temp[2][0], a[i] - dp[2][1]);
temp[2][1] = max(temp[2][1], a[i] - dp[2][0]);
}
swap(dp, temp);
REP(j, 3) {
poss[j] = (dp[j][1] > 0);
if (dp[j][0] < 0) {
if (dp[j][1] % 2 == 0)
dp[j][0] = 2;
else
dp[j][0] = 1;
}
}
// debug(dp);
// debug(poss);
}
bool win = false;
a[R] *= 2;
if (poss[2] && dp[2][0] % 2 == 0 && dp[2][0] <= a[R] && a[R] <= dp[2][1])
win = true;
if (poss[1] && dp[1][0] % 2 == 1 && dp[1][0] <= a[R] - 1 && a[R] - 1 <= dp[1][1])
win = true;
if (poss[0] && dp[0][0] % 2 == 0 && dp[0][0] == a[R] - 2)
win = true;
if (win)
cout << "TAK\n";
else
cout << "NIE\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin >> q;
while (q--)
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'; #else #define debug(...) {} #endif constexpr int inf = (int) (2e9 + 13); vector<vector<int>> dp(3, vector<int>(2)); vector<vector<int>> temp(3, vector<int>(2)); vector<bool> poss(3); void solve() { REP(i, 3) poss[i] = true; int n; cin >> n; vector<int> a(n); int L = n + 1, R = -1; REP(i, n) { cin >> a[i]; if (a[i] != 0) { L = min(L, i); R = i; } } FOR(i, L, R) { if (a[i] == 0) { cout << "NIE\n"; return; } } if (L == R) { if (a[L] == 1) cout << "TAK\n"; else cout << "NIE\n"; return; } dp[0][0] = dp[0][1] = 2 * a[L]; dp[1][0] = dp[1][1] = 2 * a[L] - 1; dp[2][0] = dp[2][1] = 2 * a[L] - 2; if (dp[2][0] == 0) poss[2] = false; // debug(dp); // debug(poss); FOR(i, L + 1, R - 1) { REP(j, 3) { temp[j][0] = inf; temp[j][1] = -inf; } a[i] *= 2; if (poss[0]) { temp[0][0] = temp[0][1] = a[i] - dp[0][0]; temp[1][0] = temp[1][1] = temp[0][0] - 1; temp[2][0] = temp[2][1] = temp[0][0] - 2; } if (poss[1]) { temp[1][0] = min(temp[1][0], a[i] - dp[1][1]); temp[1][1] = max(temp[1][1], a[i] - dp[1][0]); temp[2][0] = min(temp[2][0], a[i] - dp[1][1] - 1); temp[2][1] = max(temp[2][1], a[i] - dp[1][0] - 1); } if (poss[2]) { temp[2][0] = min(temp[2][0], a[i] - dp[2][1]); temp[2][1] = max(temp[2][1], a[i] - dp[2][0]); } swap(dp, temp); REP(j, 3) { poss[j] = (dp[j][1] > 0); if (dp[j][0] < 0) { if (dp[j][1] % 2 == 0) dp[j][0] = 2; else dp[j][0] = 1; } } // debug(dp); // debug(poss); } bool win = false; a[R] *= 2; if (poss[2] && dp[2][0] % 2 == 0 && dp[2][0] <= a[R] && a[R] <= dp[2][1]) win = true; if (poss[1] && dp[1][0] % 2 == 1 && dp[1][0] <= a[R] - 1 && a[R] - 1 <= dp[1][1]) win = true; if (poss[0] && dp[0][0] % 2 == 0 && dp[0][0] == a[R] - 2) win = true; if (win) cout << "TAK\n"; else cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; while (q--) solve(); return 0; } |
English