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

Submit Status Practice CodeForces 245D

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 018 -1 00 0 -1

Output

18 18 0 

Input

4-1 128 128 128128 -1 148 160128 148 -1 128128 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 }
时间: 2024-07-30 16:40:04

F - Restoring Table(按位与,按位或)的相关文章

Java中逻辑与,逻辑或,按位与,按位或的区分

一直以来,对这样的概念很是模糊,今天总结了一下 先说说逻辑与(&&),逻辑或(||) 他们是短路形式的,举例说明 int i = 0;    System.out.println ( i++ == 1 && i++ == 2);//打印false    System.out.println (i);//打印1 //这里先判断i++是否等于1,因为是右自增,所以这里i++ == 1是错误的,短路就是说不用执行后面的i++ == 2了,   直接返回一个false,所以这就是最

位与&amp;, 位或| ,位异或 ^ 总结

目录 按位与运算符(&) 参加运算的两个数据,按二进制位进行“与”运算. 运算规则:0&0=0;  0&1=0;   1&0=0;    1&1=1; 即:两位同时为“1”,结果才为“1”,否则为0 例如:3&5  即 0000 0011& 0000 0101 = 00000001  因此,3&5的值得1. 另,负数按补码形式参加按位与运算. “与运算”的特殊用途: (1)清零.如果想将一个单元清零,即使其全部二进制位为0,只要与一个各位都为

判断回文,位与,位或

//判断字符串是否是回文? int fun(char *sre) { char *s1,*s2; s1 = str; s2=str+strlen(str)-1; if(strlen(str)%2 != 0) { return 0; } while(s1<=2) { if(*s1==*s2) { s1++; s2--; } else return 0; } return 1; } //讲一个字符串转化为十进制数("123456" to 123456,字符串中只有数字,没有 //没有

Codeforces Round #451 (Div. 2) F Restoring the Expression

题意: 有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数 给出这个数,要求还原等式,length <= 1e6 三个数不能含有前导0,保证有解 解法: 铁头过题法,分类然后各种判断 我分了5种情况 0.开头字符为0, 那么结果一定是0+a=a的形式 然后4种情况 1.len(a) >= len(b) 且 len(c) == len(a) 2.len(a) <= len(b) 且 len(c) == len(b) 3.len(a) >= len(b) 且 len(c)

F - Restoring the Expression CodeForces - 898F

字符串hash:  base设置为10 枚举'='可能出现的位置,从1/2处开始到大概1/3处结束,当然大概的1/3不用计算,直接到最后就行,因为本题必然有解,输出直接结束即可. 根据'='号位置,'+'最多有四种位置,因为 等式的和位数确定,有进位和不进位,左和右,最多2X2,然后剪掉j的非法位置(这里没计算除了len=3以外的j有无非法位置的可能,剪了再说) 比较需要注意的地方是等式中非个位数字不能以'0'开头,开始只以为不以'0'开头即可,WA在了 1+0=0 ,改了j后又WA了0+0=0

5.1个人赛解题报告(区间dp,按位与或,图论等水题)

这次5.1打了一场个人赛,已经连赛了三周了,有点疲惫感觉,可能自己太水了,每次都有点小紧张. 这次只解出来三道题,然而有一道按位与按位或的水题不知道思路实在是做题太少,还有就是第一题区间DP,也消耗了不少的时间,但是没有成功的写出来,还是不够熟练啊. 下面写报告 A. System Administrator time limit per test 2 seconds memory limit per test 256 megabytes input standard input output

Java 位运算(移位、位与、或、异或、非)与逻辑运算

java 位运算包括:左移( << ).右移( >> ) .无符号右移( >>> ) .位与( & ) .位或( | ).位非( ~ ).位异或( ^ ),除了位非( ~ )是一元操作符外,其它的都是二元操作符. 逻辑运算符&.&&.|.||: 一.逻辑&与短路&&的区别 总的来说区别是体现在,只有这两个运算符的左边为false的时候会有区别,看如下代码 1.逻辑&的运算 boolean a = tr

【翻译】Flink Table 和 SQL API 概念与通用API

本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/common.html Table API和SQL集成在共同API中.该API的中心概念是Table,用作查询的输入和输出.本文档介绍了使用Table API和SQL查询的程序的通用结构,如何注册 Table,如何查询Table以及如何发出 Table(数据). 两个 planner 之间的主要区别 表API和SQL程序的结构 创建一个Tab

Hive入门到剖析(一)

1 Hive简介 1.1 Hive定义 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能. 本质是将SQL转换为MapReduce程序. 1.2 为什么使用Hive 1.面临的问题 人员学习成本太高 项目周期要求太短 我只是需要一个简单的环境 MapReduce  如何搞定 复杂查询好难 Join如何实现 2.为什么要使用Hive 操作接口采用类SQL语法,提供快速开发的能力 避免了去写MapReduce,减少开发人员的学习成本 扩展