Codeforces Round #553 (Div. 2) B题 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.

 

思路:搜索+剪枝 (我们只需要记录之前每行出现过的异或合即可)

 

#include<bits/stdc++.h>
using namespace std;
int tu[505][505];
int vis[505][1025];
int ans[505];
int n,m,flag;
void dfs(int row,int cur){
	//cout<<cur<<" "<<row<<endl;
	if(vis[row][cur]) return; 
	vis[row][cur] = 1;
	if(row>n){
		if(cur==0) return;
		flag = 1;
		return ;
	}
	for(int i=1;i<=m;i++){
		if(row==1){
			ans[row] = i;
			dfs(row+1,tu[row][i]);
			if(flag) return;
		}else{
			ans[row] = i;
			dfs(row+1,cur^tu[row][i]);
			if(flag) return;
		} 
	}
 
	return ;
}
int main(){
    ios::sync_with_stdio(false);
	cin>>n>>m;
	flag = 0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>tu[i][j];
		}
	}	
	dfs(1,tu[1][1]);
	if(!flag) cout<<"NIE"<<endl;
	else{
		cout<<"TAK"<<endl;
		for(int i=1;i<n;i++){
			cout<<ans[i]<<" ";
		}
		cout<<ans[n]<<endl;
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值