F - Restoring Table(按位与,按位或)

这题坑爹。。按位没学好。&是同为1,结果才是1,而|是同为0,结果才是0,这样num1&num2,num1&num3,num1&num4,得到的结果里的二进制含有1的位数,则在原数num1和num2.3.4里肯定是1,含有0的位数,要么在原数中是0,要么在num2.3.4中是0,或者都是0
 
所以用|运算,把有1的位数(原数中肯定此位是1)都先还原回去,然后多次操作叠加起来。就能还原出一组可能的数据了(注意:只是可能的一组数据,因为会有多组解,无法都找到)
 
F - Restoring Table
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.

For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we'll denote it as bij) equals:

  • the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j;
  • -1, if i = j.

Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.

Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.

Input

The first line contains a single integer n(1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i(1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j(1 ≤ i, j ≤ ni ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji.

Output

Print n non-negative integers a1, a2, ..., an(0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.

It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.

Sample Input

Input
1
-1
Output
0 
Input
3
-1 18 0
18 -1 0
0 0 -1
Output
18 18 0 
Input
4
-1 128 128 128
128 -1 148 160
128 148 -1 128
128 160 128 -1
Output
128 180 148 160 

Hint

If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.

 

下附代码

 

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 char s[5001];
 9 int a[500][500]={0};
10 int b[501]={0};
11 int main()
12 {
13     int n,i,j;
14     scanf("%d",&n);
15     for(i=0;i<n;i++)
16         for(j=0;j<n;j++)
17         {
18             scanf("%d",&a[i][j]);
19             if(i!=j)
20                 b[i]|=a[i][j];
21         }
22     for(i=0;i<n-1;i++)
23         printf("%d ",b[i]);
24     printf("%d\n",b[n-1]);
25     return 0;
26 }

 

posted @ 2015-05-02 11:04  kingofprank  阅读(191)  评论(0编辑  收藏  举报