Arab Collegiate Programming Contest 2015 E. The Minions Quiz(思维)

The minions have finally found their new master. This time, he is a Math professor and he is trying veryhard to teach them math. He has been teaching them bitwise operators for over a year! They learnt aboutAND(&) and OR (|) operators and it is time for a quiz to test them. 

The quiz is very simple, they will be given a number A of AND(&) operators, a number B of OR (|)operators and (A + B + 1) integers. They have to find the maximum number that can be obtained byinserting the & and | operators between the given nonnegative integers without changing their order. 

 Finally, there is a special requirement for this quiz, they are required to evaluate the operators from leftto right. 

 Input 

 The first line of the input will be a single integer T, the number of test cases (1 ≤ T ≤ 100), followedby T test cases.Each test case will consist of 2 lines. The first line will contain 2 integers A and B (0 ≤ A, B ≤ 10, 000)representing the number of AND(&) and OR (|) operators, respectively. The second line of input willconsist of (A + B + 1) 64-bit nonnegative integers separated by single spaces.

 Output

 For each test case, output a single line containing the maximum number that can be obtained by insertingthe operators between the given integers. 

样例输入复制

2
1 1
1 4 5
2 2
2 3 11 4 5

样例输出复制

5
7

题意:

在a + b + 1个数之间插入 a 个 & 号和 b 个 | 号,问可以得到的最大值

题解:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e4 +10;

int p[N];

int main()
{
    int n, t, a, b;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &a, &b);
        n = a + b + 1;
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d", &p[i]);
        }
        ll ans = p[1];
        for(int i = 2; i <= n; ++i)
        {
            if(a)
            {
                ans &= p[i];
                a--;
            }
            else
            {
                ans |= p[i];
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值