Codeforces Round #553 (Div. 2)B. Dima and a Bad XOR

B. Dima and a Bad XOR

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Student Dima from Kremland has a matrix aa of size n×mn×m filled with non-negative integers.

He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!

Formally, he wants to choose an integers sequence c1,c2,…,cnc1,c2,…,cn (1≤cj≤m1≤cj≤m) so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0a1,c1⊕a2,c2⊕…⊕an,cn>0holds, where ai,jai,j is the matrix element from the ii-th row and the jj-th column.

Here x⊕yx⊕y denotes the bitwise XOR operation of integers xx and yy.

Input

The first line contains two integers nn and mm (1≤n,m≤5001≤n,m≤500) — the number of rows and the number of columns in the matrix aa.

Each of the next nn lines contains mm integers: the jj-th integer in the ii-th line is the jj-th element of the ii-th row of the matrix aa, i.e. ai,jai,j (0≤ai,j≤10230≤ai,j≤1023).

Output

If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE".

Otherwise print "TAK" in the first line, in the next line print nn integers c1,c2,…cnc1,c2,…cn (1≤cj≤m1≤cj≤m), so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0a1,c1⊕a2,c2⊕…⊕an,cn>0 holds.

If there is more than one possible answer, you may output any.

Examples

input

Copy

3 2
0 0
0 0
0 0

output

Copy

NIE

input

Copy

2 3
7 7 7
7 7 10

output

Copy

TAK
1 3 

Note

In the first example, all the numbers in the matrix are 00, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero.

In the second example, the selected numbers are 77 (the first number in the first line) and 1010 (the third number in the second line), 7⊕10=137⊕10=13, 1313 is more than 00, so the answer is found.

分析:简单dp,用dp[i][k]表示第i行选择第dp[i][k]个异或值为k。跑个3重循环遍历一遍,最后在跑一遍路径。

#include "bits/stdc++.h"

using namespace std;

int dp[504][1100];
int a[504][504];

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            scanf("%d", &a[i][j]);
        }
    }
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
        for (int k = 0; k < 1024; ++k) {
            for (int j = 1; j <= m; ++j) {
                if (dp[i - 1][k ^ a[i][j]] != -1) {
                    dp[i][k] = j;
                }
            }
        }
    }
    for (int i = 1; i < 1024; ++i) {
        if (dp[n][i] != -1) {
            vector<int> ans;
            int base = i;
            for (int j = n; j >= 1; --j) {
                ans.push_back(dp[j][base]);
                base ^= a[j][dp[j][base]];
            }
            puts("TAK");
            for (int i = ans.size() - 1; i >= 0; --i) {
                printf("%d ", ans[i]);
            }
            return 0;
        }
    }
    puts("NIE");
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/89415371
今日推荐