#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
void run();
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(12);
run();
cout << flush; _Exit(0);
}
void solve() {
int n; cin >> n;
deque<int> vals(n);
each(v, vals) cin >> v;
while (!vals.empty() && vals.back() == 0) vals.pop_back();
while (!vals.empty() && vals.front() == 0) vals.pop_front();
n = sz(vals);
ll lim = 0;
bool dp[2][2][2][2][2] = {};
dp[1][0][0][0][0] = 1;
rep(i, 0, n-1) {
auto &cur = dp[i%2], &prv = dp[1-i%2];
memset(cur, 0, sizeof(cur));
lim = vals[i] - lim;
rep(a, 0, 2) rep(b, 0, 2) if (a-b <= lim) {
rep(c, 0, 2-b) rep(d, 0, 2-a) if (c-d <= lim && a-b+c-d < lim*2) {
rep(e, 0, a+1) rep(f, 0, c+1) {
cur[d][c][b][a] = cur[d][c][b][a] || prv[a-e][b][c-f][d];
}
}
}
}
auto &prv = dp[n%2];
lim = vals[n-1] - lim;
rep(a, 0, 2) rep(b, 0, 2) if (a-b == lim) {
int c = 1-b, d = 1-a;
rep(e, 0, a+1) rep(f, 0, c+1) {
if (prv[a-e][b][c-f][d]) {
cout << "TAK\n";
return;
}
}
}
cout << "NIE\n";
}
void run() {
int t; cin >> t;
while (t--) solve();
}
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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define deb(...) #define DBP(...) using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); run(); cout << flush; _Exit(0); } void solve() { int n; cin >> n; deque<int> vals(n); each(v, vals) cin >> v; while (!vals.empty() && vals.back() == 0) vals.pop_back(); while (!vals.empty() && vals.front() == 0) vals.pop_front(); n = sz(vals); ll lim = 0; bool dp[2][2][2][2][2] = {}; dp[1][0][0][0][0] = 1; rep(i, 0, n-1) { auto &cur = dp[i%2], &prv = dp[1-i%2]; memset(cur, 0, sizeof(cur)); lim = vals[i] - lim; rep(a, 0, 2) rep(b, 0, 2) if (a-b <= lim) { rep(c, 0, 2-b) rep(d, 0, 2-a) if (c-d <= lim && a-b+c-d < lim*2) { rep(e, 0, a+1) rep(f, 0, c+1) { cur[d][c][b][a] = cur[d][c][b][a] || prv[a-e][b][c-f][d]; } } } } auto &prv = dp[n%2]; lim = vals[n-1] - lim; rep(a, 0, 2) rep(b, 0, 2) if (a-b == lim) { int c = 1-b, d = 1-a; rep(e, 0, a+1) rep(f, 0, c+1) { if (prv[a-e][b][c-f][d]) { cout << "TAK\n"; return; } } } cout << "NIE\n"; } void run() { int t; cin >> t; while (t--) solve(); } |
English