旺崽的博客

要么天赋异禀,要么天道酬勤

0%

Codeforces Round 644 (Div. 3) H.Binary Median

题目链接

Consider all binary strings of length m (1≤m≤60). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly 2m such strings in total.

The string s is lexicographically smaller than the string t (both have the same length m) if in the first position i from the left in which they differ, we have s[i]<t[i]. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second.

We remove from this set $n (1≤n≤min(2^{m−1},100))$ distinct binary strings a1,a2,…,an, each of length m. Thus, the set will have k=2m−n strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary).

We number all the strings after sorting from 0 to k−1. Print the string whose index is ⌊k−1⌋/2 (such an element is called median), where ⌊x⌋ is the rounding of the number down to the nearest integer.

For example, if n=3, m=3 and a=[010, 111, 001], then after removing the strings ai and sorting, the result will take the form: [000, 011, 100, 101, 110]. Thus, the desired median is 100.

Input

The first line contains an integer t (1≤t≤1000) — the number of test cases. Then, t test cases follow.

The first line of each test case contains integers $n (1≤n≤min(2^{m−1},100))$ and m (1≤m≤60), where n is the number of strings to remove, and m is the length of binary strings. The next n lines contain a1,a2,…,an — distinct binary strings of length m.

The total length of all given binary strings in all test cases in one test does not exceed $10^5$.

Output

Print t answers to the test cases. For each test case, print a string of length m — the median of the sorted sequence of remaining strings in the corresponding test case.

Example

input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
5
3 3
010
001
111
4 3
000
111
100
011
1 1
1
1 1
0
3 2
00
01
10

output

1
2
3
4
5
100
010
0
1
11

这题用到了位运算,我们把所有二进制字符串转化为十进制存入数组 $a$,考虑到内存限制,$bitset$ 恰好完美地解决了这个问题,我们先定位要找的那个数的位置 $pos$,也即十进制里面的大小,然后遍历数组 $a$,如果有数小于等于 $pos$,则 $pos+1$,最后再将其转化为二进制输出即可,AC代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

void solve() {
int n, m;
cin >> n >> m;
ll a[n];
string s;
ll ans = ((1LL << m) - n - 1) / 2;
for (int i = 0; i < n; i++) {
cin >> s;
a[i] = bitset<64>(s).to_ullong();
}
sort(a, a + n);
for (auto i:a) ans += (i <= ans);
cout << bitset<64>(ans).to_string().substr(64 - m) << endl;
}

int main() {
int t;
cin >> t;
while (t--) solve();
}