Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- int main(){
- fast;
- ll t,tcno=0;cin>>t;
- while(t--){
- ll n,m;cin>>n>>m;
- ll a[n];
- ll b = 0;
- for(ll i=0;i<n;++i){
- cin>>a[i];
- //b = max(b, (ll)log2l(a[i])+1);
- for(ll j=50;j>=0;--j){
- if(a[i] & (1ll<<j)){
- b = max(b, 1+j);
- break;
- }
- }
- }
- ll ans = 0;
- for(ll i=b-1;i>=0;--i){
- ll tot = 0;
- for(ll j=0;j<n;++j){
- if(a[j] & (1ll << i)){
- ++tot;
- }
- }
- //cout<<"\nres="<<tot<<endl;
- if(tot>n/2){
- ans += 1ll << i;
- }
- }
- ll r = 0;
- for(ll i=0;i<n;++i){
- r+= ans ^ a[i];
- }
- if(r>m){
- cout<<"Case #"<<++tcno<<": "<<-1<<endl;
- continue;
- }
- //cout<<"preans"<<ans<<endl;
- for(ll i=b-1;i>=0;--i){
- if((ans & (1ll << i))==0){
- ll res = 0;
- ll x = ans + (1ll <<i);
- for(ll j=0;j<n;++j){
- res += x ^ a[j];
- }
- if(res<=m){
- ans += (1ll << i);
- }
- }
- }
- cout<<"Case #"<<++tcno<<": "<<ans<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment