Codeforces Round #344 (Div. 2)(按位或运算)

Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.

We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays.

The second line contains n integers ai (0 ≤ ai ≤ 109).

The third line contains n integers bi (0 ≤ bi ≤ 109).

Output

Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

Examples

Input

51 2 4 3 22 3 3 12 1

Output

22

Input

1013 2 7 11 8 4 9 8 5 15 7 18 9 2 3 0 11 8 6

Output

46

Note

Bitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.

In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.

In the second sample, the maximum value is obtained for l = 1 and r = 9.

题意(虽然题目很水,不过位运算太弱了,还是写了一下):

函数f(a, l, r)表达式为:al | al+1 | al+2 | ...... | ar,其中a为一个集合,ai表示集合a中第i个元素;

现有集合a , b (都有n个元素),求f(a, l, r) + f(b, l , r)的最大值;

思路:首先我想到的就是两个for找l和r,因为数据只有1000,肯定是不会超时的;

不过我对位运算并不熟悉,所以先按样例解析跑了一遍。。

发现2 | 4 | 3 = 1 | 2 | 4 | 3 = 2 | 4 | 3 | 2 = 1 | 2 | 4 | 3 | 2 !!!!很神奇有木有!!!!

由此猜测对任意一个集合的所有子集的所有按位或运算。。最大值等于这个集合本身所有元素间进行位运算。。

样例2也符合这个规律。。。

仔细一想。。不难发现任意两个数进行位或运算后得到的值必然是非减的。。这可以由位或运算的规则证明。。

位或其功能是参与运算的两数各对应的二进位相或。只要对应的二个二进位有一个为1时,结果位就为1。2进制对应位只要有一个为1,其结果就是1.。所有运算结果不可能比原数小。。。

所有这个题目只需要分别把两个集合的元素进行位或运算再相加就可以了啦。。。

代码:

#include <bits/stdc++.h>
#define ll long long
#define MAXN 100000+10
using namespace std;

int main(void)
{
    int n, x, y, cnt1=0, cnt2=0;   // 注意cnt1, cnt2赋初值0,如果赋值1的话其结果可能变大
    cin >> n;
    for(int i=0; i<n; i++)
    {
        cin >> x;
        cnt1|=x;
    }
    for(int j=0; j<n; j++)
    {
        cin >> y;
        cnt2|=y;
    }
    cout << cnt1+cnt2 << endl;
    return 0;
}

时间: 2024-10-12 04:23:04

Codeforces Round #344 (Div. 2)(按位或运算)的相关文章

Codeforces Round #344 (Div. 2) C. Report

Report 题意:给长度为n的序列,操作次数为m:n and m (1 ≤ n, m ≤ 200 000) ,操作分为t r,当t = 1时表示将[1,r]序列按非递减排序,t = 2时表示将序列[1,r]按非递增排序:输出m次操作后的序列? 思路:由于排序是前缀排序,那么前面的操作ti,ri;如果 ri <= rj;那么第i次操作直接被覆盖了.这样我们可以知道有用的排序操作ri是按照严格递减的:并且这时在ri 与r(i + 1)之间的数与后面的操作无关了~~(线性处理),这就直接说明了只需要

Codeforces Round #344 (Div. 2)

水 A - Interview 注意是或不是异或 #include <bits/stdc++.h> int a[1005], b[1005]; int main() { int n; scanf ("%d", &n); for (int i=0; i<n; ++i) { scanf ("%d", a+i); } for (int i=0; i<n; ++i) { scanf ("%d", b+i); } int a

Codeforces Round #344 (Div. 2) C. Report 水题

#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define rep(i,a,b) for(int i=a;i>=b;i--) #define RI(x) scanf("%d",&x) #define RII(x,y) scanf("%d%d",&x,&y) #d

Codeforces Round #344 (Div. 2) E. Product Sum 二分斜率优化DP

E. Product Sum Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special. You are given an array a of

Codeforces Round #344 (Div. 2) 631 C. Report (单调栈)

C. Report time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities

图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

题目传送门 1 /* 2 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 3 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 4 关键在于更新异或和,精髓:a ^ b = c -> a ^ c = b, b ^ c = a; 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <algorith

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #258 (Div. 2)

A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除交点所在的行和列,则剩下n-1行和m-1列,直到行或列被删完为止,最多删除的次数为min(n,m),删除min(n,m)后剩余的都是行或者列(注意行与行,列与列之间不可能有交点).只需要判断min(n,m)的奇偶性. #include <iostream> #include <vector&