Codeforces Global Round 21(只会俩题)

A. NIT orz!

time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to or𝑧or⁡z. Can you solve this problem too?

You are given a 1-indexed array of 𝑛n integers, 𝑎a, and an integer 𝑧z. You can do the following operation any number (possibly zero) of times:

  • Select a positive integer 𝑖i such that 1≤𝑖≤𝑛1≤i≤n. Then, simutaneously set 𝑎𝑖ai to (𝑎𝑖or𝑧)(aior⁡z) and set 𝑧z to (𝑎𝑖and𝑧)(aiand⁡z). In other words, let 𝑥x and 𝑦y respectively be the current values of 𝑎𝑖ai and 𝑧z. Then set 𝑎𝑖ai to (𝑥or𝑦)(xor⁡y) and set 𝑧z to (𝑥and𝑦)(xand⁡y).

Here oror and andand denote the bitwise operations OR and AND respectively.

Find the maximum possible value of the maximum value in 𝑎a after any number (possibly zero) of operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases 𝑡t (1≤𝑡≤1001≤t≤100). Description of the test cases follows.

The first line of each test case contains two integers 𝑛n and 𝑧z (1≤𝑛≤20001≤n≤2000, 0≤𝑧<2300≤z<230).

The second line of each test case contains 𝑛n integers 𝑎1a1,𝑎2a2,……,𝑎𝑛an (0≤𝑎𝑖<2300≤ai<230).

It is guaranteed that the sum of 𝑛n over all test cases does not exceed 104104.

Output

For each test case, print one integer — the answer to the problem.

Example

input

Copy

5
2 3
3 4
5 5
0 2 4 6 8
1 9
10
5 7
7 15 30 29 27
3 39548743
10293834 10284344 13635445

output

Copy

7
13
11
31
48234367

Note

In the first test case of the sample, one optimal sequence of operations is:

  • Do the operation with 𝑖=1i=1. Now 𝑎1a1 becomes (3or3)=3(3or⁡3)=3 and 𝑧z becomes (3and3)=3(3and⁡3)=3.
  • Do the operation with 𝑖=2i=2. Now 𝑎2a2 becomes (4or3)=7(4or⁡3)=7 and 𝑧z becomes (4and3)=0(4and⁡3)=0.
  • Do the operation with 𝑖=1i=1. Now 𝑎1a1 becomes (3or0)=3(3or⁡0)=3 and 𝑧z becomes (3and0)=0(3and⁡0)=0.

After these operations, the sequence 𝑎a becomes [3,7][3,7], and the maximum value in it is 77. We can prove that the maximum value in 𝑎a can never exceed 77, so the answer is 77.

In the fourth test case of the sample, one optimal sequence of operations is:

  • Do the operation with 𝑖=1i=1. Now 𝑎1a1 becomes (7or7)=7(7or⁡7)=7 and 𝑧z becomes (7and7)=7(7and⁡7)=7.
  • Do the operation with 𝑖=3i=3. Now 𝑎3a3 becomes (30or7)=31(30or⁡7)=31 and 𝑧z becomes (30and7)=6(30and⁡7)=6.
  • Do the operation with 𝑖=5i=5. Now 𝑎5a5 becomes (27or6)=31(27or⁡6)=31 and 𝑧z becomes (27and6)=2(27and⁡6)=2.

代码如下:

#include <iostream>
#include <vector>

using namespace std;

const int N = 1e05 + 10;

int main() {
	int _;
	cin >> _;
	while (_ --) {
		int n, m;
		cin >> n >> m;
		vector<int> q(n);
		for (int i = 0; i < n; i++) cin >> q[i];
		int maxn = 0;
		for (int i = 0; i < n; i++) {
			maxn = max(maxn, q[i] | m);
		}

		cout << maxn << endl;
	}

	return 0;
}

 按位与很显然只能比原来的数字小,所以说z的数值是越来越小的,然后两个数按位或的话只能比这两个数都大,所以说问题就来了:

How many operations will we perform?

The answer is at most one!

Suppose we can only perform exactly one operation. In this case the answer is 𝑆=max1≤𝑖≤𝑛(𝑎𝑖 or 𝑧)S=max1≤i≤n(ai or z). In fact, we can prove that this is the answer.

Define 𝑎′𝑖ai′ as the value of 𝑎𝑖ai after some operations.

It suffices to prove the answer will never exceed 𝑆S. Note that 𝑧z will always become a submask of itself after any number of operations, so 𝑎𝑖ai will always be a submask of (𝑎𝑖 or 𝑧)(ai or z) after any number of operations. This leads to the conclusion that 𝑎′𝑖≤(𝑎𝑖 or 𝑧)ai′≤(ai or z) for all 𝑖i. Thus max1≤𝑖≤𝑛𝑎′𝑖≤max1≤𝑖≤𝑛(𝑎𝑖 or 𝑧)=𝑆max1≤i≤nai′≤max1≤i≤n(ai or z)=S.

Time complexity is 𝑂(𝑛)O(n).

所以我们只能进行一次变换,找最大的。

B:

B. NIT Destroys the Universe

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

For a collection of integers 𝑆S, define mex(𝑆)mex⁡(S) as the smallest non-negative integer that does not appear in 𝑆S.

NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.

The universe can be represented as a 1-indexed array 𝑎a of length 𝑛n. When NIT snaps his fingers, he does the following operation on the array:

  • He selects positive integers 𝑙l and 𝑟r such that 1≤𝑙≤𝑟≤𝑛1≤l≤r≤n. Let 𝑤=mex({𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟})w=mex⁡({al,al+1,…,ar}). Then, for all 𝑙≤𝑖≤𝑟l≤i≤r, set 𝑎𝑖ai to 𝑤w.

We say the universe is destroyed if and only if for all 1≤𝑖≤𝑛1≤i≤n, 𝑎𝑖=0ai=0 holds.

Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to 00.

Input

Each test contains multiple test cases. The first line contains the number of test cases 𝑡t (1≤𝑡≤1041≤t≤104). Description of the test cases follows.

The first line of each test case contains one integer 𝑛n (1≤𝑛≤1051≤n≤105).

The second line of each test case contains 𝑛n integers 𝑎1a1, 𝑎2a2, ……, 𝑎𝑛an (0≤𝑎𝑖≤1090≤ai≤109).

It is guaranteed that the sum of 𝑛n over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print one integer — the answer to the problem.

Example

input

Copy

4
4
0 0 0 0
5
0 1 2 3 4
7
0 2 3 0 1 2 0
1
1000000000

output

Copy

0
1
2
1

Note

In the first test case, we do 00 operations and all elements in the array are already equal to 00.

In the second test case, one optimal way is doing the operation with 𝑙=2l=2, 𝑟=5r=5.

In the third test case, one optimal way is doing the operation twice, respectively with 𝑙=4l=4, 𝑟=4r=4 and 𝑙=2l=2, 𝑟=6r=6.

In the fourth test case, one optimal way is doing the operation with 𝑙=1l=1, 𝑟=1r=1.

code:

#include <iostream>
#include <vector>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
  	cin.tie(0);
	int _;
	cin >> _;
	while (_ --) {
		int n, m;
		cin >> n;
		vector<int> q(n + 1);
		for (int i = 0; i < n; i++) cin >> q[i];
		int ans = 0;
		for (int i = 0; i < n; i++) ans += (q[i] > 0);
		for (int i = 0; i < n - 1; i++) ans -= (q[i] > 0 && q[i + 1] > 0);
		ans = min(2, ans);
		cout << ans << endl; 
	}
	return 0;
}

答案只有012三种。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值