1775C - Interesting Sequence - CodeForces Solution


bitmasks math

Please click on ads to support us..

Python Code:

def find_m(n, x):
    f = [0] * 60
    for i in range(60):
        if (~n >> i) & 1:
            f[i] = n
        else:
            f[i] = (n & ~((1 << i) - 1)) + (1 << i)

    m = n
    for i in range(60):
        if (~x >> i) & 1:
            m = max(m, f[i])

    for i in range(60):
        if (x >> i) & 1:
            if m >= f[i]:
                return -1
    return m

def main():
    t = int(input())
    for i in range(t):
        n, x = map(int, input().split())
        result = find_m(n, x)
        print(result)

if __name__ == '__main__':
    main()

C++ Code:

//Rakib Hasan

#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using    namespace __gnu_pbds;
using    namespace std;
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define optimize()  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define mod  998244353 //1000000007
const long long infll=9000000000000000000;
const int inf = 2000000000;
const double eps=1e-9;


#define                    ll                   long long
#define                    pa                   pair<int,int>
#define                    ff                   first
#define                    ss                   second
#define                    pb                   push_back
#define                    PI                   acos(-1.0)
#define                    vi                   vector<int>
#define                    vll                  vector<ll>
#define                    yo                   cout<<"YES"<<endl
#define                    no                   cout<<"NO"<<endl
#define                    done                 cout<<ans<<endl
#define                    mem(a,b)             memset(a,b,sizeof(a))
#define                    minus                cout<<"-1"<<endl
#define                     duck                cout<<0<<endl
#define                    sqr(a)               ((a)*(a))
#define                    f(i,a,b)             for(int i=a;i<=(b);i++)
#define                    r(i,a,b)             for(int i=a;i>=(b);i--)
#define                    all(v)               v.begin(),v.end()
#define                    vpush(v,x)           int x; cin>>x; v.push_back(x);
#define                    mid(s,l)             (s+(l-s)/2)



//#define int long long


void solve()
{
   ll n,x;
   cin>>n>>x;
   if(n<x){
    cout<<-1<<endl;
    return;
   }
   if(n==x){
    cout<<n<<endl;
    return;
   }
   if(x==0){
    cout<<(1LL<<((ll)log2(n)+1LL))<<endl;
   }
   
   bitset<66>bn(n);
   bitset<66>bx(x);
   bool check=false;

   for(int i=0;i<=65;i++){
    if(bn[i]==1){
        if(bx[i]==0 and check){
            cout<<-1<<endl;
            return;
        }
        else if(bx[i]==1)check=true;
    }
    else if(bx[i]==1)check =true;
   }
    ll num=0;
    ll ans=n;
    ll last;


   for(int i=0;i<=65;i++){
    if(bx[i]==1){
        if(last==(1LL<<(ll)i)){
            cout<<-1<<endl;
            return;
        }
        ans+=(last-num);
        cout<<ans<<endl;
        return;
    }
    if(bn[i]==1){
        num+=(1LL<<(ll)i);
        last=(1LL<<((ll)i+1LL));

    }
   }


}

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


Comments

Submit
0 Comments
More Questions

Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum