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
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define INT(x) int x; scanf("%d", &x)

typedef vector<bool> VB;

int main() {
	INT(n);
	INT(m);
	INT(k);
	VB a(n * m);
	int x = 0;
	REP(kk,k) {
		INT(r);
		INT(c);
		INT(z);
		r = (r ^ x) % n;
		c = (c ^ x) % m;
		a[r * m + c] = 1;
		VB b(n * m);
		queue<int> q;
		b[0] = 1;
		q.push(0);
		while (!q.empty()) {
			int p = q.front();
			q.pop();
			if (p < (n - 1) * m && !a[p + m] && !b[p + m]) {
				b[p + m] = 1;
				q.push(p + m);
			}
			if (p % m < m - 1 && !a[p + 1] && !b[p + 1]) {
				b[p + 1] = 1;
				q.push(p + 1);
			}
		}
		if (!b[n * m - 1]) {
			a[r * m + c] = 0;
			x ^= z;
			printf("TAK\n");
		} else printf("NIE\n");
	}
}