#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 1000005
#define inf 0x3f3f3f3f
int n,a[maxn];
map<int,int>f[3],g[3];
int C(int n,int m){
return m>=0 && n-m>=0;
}
void work()
{
n=read();
For(i,0,2) f[i].clear(),g[i].clear();
For(i,1,n)a[i]=read();
while(n && !a[n])--n;
int L=1;
while(L<=n && !a[L])++L;
For(i,1,n-L+1) a[i]=a[i+L-1];
n-=(L-1);
if(!n){
puts("TAK");
return;
}
f[0][a[1]]=1;
For(i,2,n){
for(auto t:f[0]){
int j=t.fi;
int w=t.se>0;
g[0][a[i]-j]+=w*C(a[i]-1,j);
g[1][a[i]-j+1]+=w*C(a[i]-1,j-1)*2;
g[2][a[i]-j+2]+=w*C(a[i]-1,j-2);
}
for(auto t:f[1]){
int j=t.fi;
int w=t.se>0;
g[1][a[i]-j+1]+=w*C(a[i]-1,j-1);
g[2][a[i]-j+2]+=w*C(a[i]-1,j-2);
}
for(auto t:f[2]){
int j=t.fi;
int w=t.se>0;
g[2][a[i]-j+2]+=w*C(a[i]-1,j-2);
}
For(j,0,2)
swap(f[j],g[j]),g[j].clear();
// For(j,0,2){
// cout<<"i,j "<<i<<" "<<j<<endl;
// for(auto t:f[j])cout<<t.fi<<" "<<t.se.x<<'\n';
// }
}
// for(auto t:f[2])
// cout<<t.fi<<' '<<t.se.x<<'\n';
int res=0;
For(i,0,2)res+=f[i][1];
if(res!=0) puts("TAK");
else puts("NIE");
}
signed main()
{
int T=read();
while(T--)work();
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 | #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);++i) #define Rep(i,a,b) for(int i=(a);i>=(b);--i) #define int long long using namespace std; inline int read() { char c=getchar();int x=0;bool f=0; for(;!isdigit(c);c=getchar())f^=!(c^45); for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48); if(f)x=-x;return x; } #define fi first #define se second #define pb push_back #define mkp make_pair typedef pair<int,int>pii; typedef vector<int>vi; #define maxn 1000005 #define inf 0x3f3f3f3f int n,a[maxn]; map<int,int>f[3],g[3]; int C(int n,int m){ return m>=0 && n-m>=0; } void work() { n=read(); For(i,0,2) f[i].clear(),g[i].clear(); For(i,1,n)a[i]=read(); while(n && !a[n])--n; int L=1; while(L<=n && !a[L])++L; For(i,1,n-L+1) a[i]=a[i+L-1]; n-=(L-1); if(!n){ puts("TAK"); return; } f[0][a[1]]=1; For(i,2,n){ for(auto t:f[0]){ int j=t.fi; int w=t.se>0; g[0][a[i]-j]+=w*C(a[i]-1,j); g[1][a[i]-j+1]+=w*C(a[i]-1,j-1)*2; g[2][a[i]-j+2]+=w*C(a[i]-1,j-2); } for(auto t:f[1]){ int j=t.fi; int w=t.se>0; g[1][a[i]-j+1]+=w*C(a[i]-1,j-1); g[2][a[i]-j+2]+=w*C(a[i]-1,j-2); } for(auto t:f[2]){ int j=t.fi; int w=t.se>0; g[2][a[i]-j+2]+=w*C(a[i]-1,j-2); } For(j,0,2) swap(f[j],g[j]),g[j].clear(); // For(j,0,2){ // cout<<"i,j "<<i<<" "<<j<<endl; // for(auto t:f[j])cout<<t.fi<<" "<<t.se.x<<'\n'; // } } // for(auto t:f[2]) // cout<<t.fi<<' '<<t.se.x<<'\n'; int res=0; For(i,0,2)res+=f[i][1]; if(res!=0) puts("TAK"); else puts("NIE"); } signed main() { int T=read(); while(T--)work(); return 0; } |
English