Martian scientists explore Ganymede, one of Jupiter's numerous moons. Recently, they have found ruins of an ancient civilization. The scientists brought to Mars some tablets with writings in a language unknown to science.
They found out that the inhabitants of Ganymede used an alphabet consisting of two letters, and each word was exactly ℓℓ letters long. So, the scientists decided to write each word of this language as an integer from 00 to 2ℓ−12ℓ−1 inclusively. The first letter of the alphabet corresponds to zero bit in this integer, and the second letter corresponds to one bit.
The same word may have various forms in this language. Then, you need to restore the initial form. The process of doing it is described below.
Denote the distance between two words as the amount of positions, in which these words differ. For example, the distance between 1001210012 and 1100211002 (in binary) is equal to two, as these words have different letters in the second and the fourth positions, counting from left to right. Further, denote the distance between words xx and yy as d(x,y)d(x,y).
Let the word have nn forms, the ii-th of which is described with an integer xixi. All the xixi are not necessarily different, as two various forms of the word can be written the same. Consider some word yy. Then, closeness of the word yy is equal to the sum of distances to each of the word forms, i. e. the sum d(xi,y)d(xi,y) over all 1≤i≤n1≤i≤n.
The initial form is the word yy with minimal possible nearness.
You need to help the scientists and write the program which finds the initial form of the word given all its known forms. Note that the initial form is not necessarily equal to any of the nn given forms.
Input
The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. The following are descriptions of the test cases.
The first line contains two integers nn and ℓℓ (1≤n≤1001≤n≤100, 1≤ℓ≤301≤ℓ≤30) — the amount of word forms, and the number of letters in one word.
The second line contains nn integers xixi (0≤xi≤2ℓ−10≤xi≤2ℓ−1) — word forms. The integers are not necessarily different.
Output
For each test, print a single integer, the initial form of the word, i. e. such yy (0≤y≤2ℓ−10≤y≤2ℓ−1) that the sum d(xi,y)d(xi,y) over all 1≤i≤n1≤i≤n is minimal possible. Note that yy can differ from all the integers xixi.
If there are multiple ways to restore the initial form, print any.
Example
input
Copy
7 3 5 18 9 21 3 5 18 18 18 1 1 1 5 30 1 2 3 4 5 6 10 99 35 85 46 78 55 2 1 0 1 8 8 5 16 42 15 83 65 78 42
output
Copy
17 18 1 1 39 0 2
Note
In the first test case, the words can be written as x1=100102x1=100102, x2=010012x2=010012 and x3=101012x3=101012 in binary. Let y=100012y=100012. Then, d(x1,y)=2d(x1,y)=2 (the difference is in the fourth and the fifth positions), d(x2,y)=2d(x2,y)=2 (the difference is in the first and the second positions), d(x3,y)=1d(x3,y)=1 (the difference is in the third position). So, the closeness is 2+2+1=52+2+1=5. It can be shown that you cannot achieve smaller closeness.
In the second test case, all the forms are equal to 1818 (100102100102 in binary), so the initial form is also 1818. It's easy to see that closeness is equal to zero in this case.
题意:输出出y,y要满足d(xi,y)最小
思路:这题直接从大体上看,直接找每一位上0多还是1多,就是相差最小的,因为题上要求的是相差和最小。
求二进制:某位是否是1用&,某位上加上1用 |
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
void solve()
{
int n,l;
cin>>n>>l;
int a[110];
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int ans=0;
for(int i=0;i<l;i++)
{
int cnt=0;
for(int j=1;j<=n;j++)
{
cnt+=(a[j]>>i)&1;
}
if(cnt>n/2) ans|=(1<<i);
}
cout<<ans<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}