SAME_AND - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: krrishmath
Tester: mexomerf
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

You are given two integers N and M.
A sequence A is called a beautiful sequence if it consists of at least two distinct integers between 0 and M such that A_i \land A_j = N for each 1 \leq i \lt j \leq |A|.

Find any beautiful sequence with maximum length.

EXPLANATION:

The bitwise AND operation will be denoted by \land.

First, observe that if we have x\land y = z then:

  1. Both x and y must be supermasks of z, i.e. every bit that’s set in z must be set in both x and y.
  2. Any bit that’s not set in z must be unset in at least one of x and y — if it’s set in both of them then it’ll be set in their bitwise AND, which contradicts the AND being equal to z.

Now, we want A_i \land A_j = N for each pair (i, j). This means:

  1. Every bit that’s set in N must be present in every element of the array.
  2. Every bit that’s not set in N can be present in at most one element of the array.

Let the bits that are not set in N be, in ascending order, b_1, b_2, b_3, \ldots
Since our aim is to maximize the number of elements of A, it’s optimal to give each element of A exactly one of the bits b_i - if we give some element two or more of the bits, we could just as well split it into multiple elements (which will both be \leq M if the original value was \leq M so that’s not an issue).

This means the elements we need to consider are

N, N+2^{b_1}, N+2^{b_2}, N+2^{b_3}, \ldots

We aren’t allowed to have elements that are \gt M, so stop the instant N + 2^{b_i} \gt M happens.

TIME COMPLEXITY:

\mathcal{O}(\log M) per testcase.

CODE:

Author's code (C++)
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n,m;
        cin>>n>>m;
        vector<ll> arr;
        arr.push_back(n);
        ll c=1;
        while(true){
            if (c<0) break;
            if ((n&c)==c) {
                c=c<<1;
                continue;
            }
            if (n+c>m) break;
            arr.push_back(n+c);
            c=c<<1;
        }
        if (arr.size()==1){
            cout<<"-1\n";
            continue;
        }
        cout<<arr.size()<<endl;
        for (auto p: arr) cout<<p<<" ";
        cout<<endl;
    }
}
Tester's code (C++)
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
	int t;
	cin>>t;
	while(t--){
	    int n, m;
	    cin>>n>>m;
	    if(n > m){
	        cout<<-1<<"\n";
	    }else{
	        int nn = n;
	        int temp = 1;
	        vector<int> ans;
	        ans.push_back(n);
	        while(n + temp <= m){
	            if(nn % 2 == 0){
	                ans.push_back(n + temp);
	            }
	            nn /= 2;
	            temp *= 2;
	        }
	        if(ans.size() == 1){
	            cout<<-1<<"\n";
	        }else{
	            cout<<ans.size()<<"\n";
	            for(int i = 0; i < ans.size(); i++){
	                cout<<ans[i]<<" ";
	            }
	            cout<<"\n";
	        }
	    }
	}
}

Editorialist's code (PyPy3)
for _ in range(int(input())):
    n, m = map(int, input().split())
    ans = [n]
    for b in range(100):
        if (n | 2**b) > m: break
        if ~n & 2**b: ans.append(n | 2**b)
    if len(ans) > 1:
        print(len(ans))
        print(*ans)
    else:
        print(-1)

Hello CodeChef Team,
Problem Name - SAME_AND
I would like to report an issue I encountered with one of your problems. I submitted my solution, and it produced the correct output for the test case “1 2” (which, according to my understanding of the problem statement, should output -1). However, the judging system expected a different output.

Below is a brief explanation:

  • Test Case: 1 2
  • Expected Output According to Problem Statement: -1
  • My Code’s Output: -1
  • Issue: The system seems to be expecting a different output despite my solution following the problem’s requirements.

I believe there might be an error with the expected output for this test case. Could you please review this case and provide clarification or update the test case if needed?

Thank you for your time and assistance.

3 Likes

same problem

same issue
i suggest submitted solutions to be judged again by their submission time

same problem

same problem

1 Like

No way my stupid ahh couldnt solve this

Nice problems, definitely a really nice bitwise problem

2 Likes

@iceknight1093 please look into this and take some action


Same problem

1 Like

how come other solutions pass with this test present i don’t understand
they are not doing anything

1 Like

That’s not the case where your code is failing. It’s just that debug feature is not working properly. You can share your submissions I will try to find what’s wrong

https://www.codechef.com/viewsolution/1139319974
this is the link
i would be very thankful if you could point out why this brute force greedy didn’t work