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
#include<bits/stdc++.h>
using namespace std;

int x, num, y, k, r, c, z;

int R() {
	return (r^num) % y;
}

int C() {
	return (c^num) % x;
}

void dfs(int a, int b, int **T, int **O) {
	O[a][b]++;
	if(a < y-1 && !T[a+1][b] && !O[a+1][b]) dfs(a+1,b, T, O);
	if(b < x-1 && !T[a][b+1] && !O[a][b+1]) dfs(a,b+1, T, O);
}

int main() {
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> y >> x >> k;
	int **t = new int*[x];
	for(int i = 0; i < x; i++)
		t[i] = new int[y];

	int **odw = new int*[x];
	for(int i = 0; i < x; i++)
		odw[i] = new int[y];
	
	
	for(int i = 0; i < k; i++) {
		cin >> r >> c >> z;
		t[R()][C()]++;
		dfs(0,0, t, odw);
		if(!odw[y-1][x-1]) {
			t[R()][C()]--;
			cout << "TAK\n";
			c = (c ^ num) % x;
			r = (r ^ num) % y;
			num = num ^ z;
		}
		else cout << "NIE\n";
		
		for(int i = 0; i < y; i++)
			for(int j = 0; j < x; j++)
				odw[i][j] = 0;
	}
	
}