304 North Cardinal St.
Dorchester Center, MA 02124

# Nil Xor CodeChef Solution

## Nil Xor CodeChef Solution in C++17

``````#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long int> vll;
typedef vector<int> vi;
typedef unordered_map<int, int> umap;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<pair<int, vi>> vpiv;
typedef unordered_map<ll, vll> ull;
typedef set<ll> sll;
typedef priority_queue<ll> pq;
#define deci(n) fixed << setprecision(n)
#define run(i,a,n) for (int i = a; i < n; i++)
#define rund(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(),v.end()
#define allr(v) v.begin(),v.end(),greater<int>()
#define in insert
#define pb push_back
#define mp(v, u) make_pair(v, u)
#define lim 200001
#define INF 1000000009
#define f first
#define s second
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
const ll mod=998244353;
const ll N = 6;
vll fact(N,1);
ll modinverse(ll a) {
ll m = mod, y = 0, x = 1;
while (a > 1) {
ll q = a / m;
ll t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += mod;
return x;
}
void allfact(){
run(i,2,N){
fact[i]=i*fact[i-1];
fact[i]%=mod;
}
}
ll ncr(ll n, ll r){
if(n<r || n<0 || r<0) return 0;
ll p=modinverse(fact[r])*modinverse(fact[n-r]);
p%=mod;
return (p*fact[n])%mod;
}
ll power(ll a, ll b, ll md = mod) {
ll product = 1;
while(b) {
if(b & 1) product = (product * a) % md;
a = (a * a) % md;
b = b >> 1;
}
return product % md;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
ll n,m,k,ans=0,res=1;
cin >> n >> m;
bitset<32>s(m);
bitset<32>r(n);
int j=log2(n);
while(j>=0){
while(j>=0 && !r[j]){
j--;
}
if(j<0){
continue;
}
if(s[j] && r[j]){
j--;
continue;
}
res=1;
run(i,0,j){
if(!s[i]){
res*=2;
}
}
ans+=res;
j--;
}
cout<<ans<<"\n";
}
}``````

## Nil Xor CodeChef Solution in C++14

``````#include <bits/stdc++.h>
//for policy based ds //p.order_of_key() -> returns index of value //*p.find_by_order(3) ->value at index
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less

using namespace __gnu_pbds; //for policy based ds
using namespace std;

#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define maxHeap priority_queue<int>;
#define minHeap priority_queue<int, vi, greater<int>>
#define mod 1000000007
#define inf 1e18
#define rep(i, s, n) for (int i = s; i < n; i++)
#define sp(ans, pre) fixed << setprecision(pre) << y
#define pb push_back
#define srt(v) sort(v.begin(), v.end())
#define all(v) begin(v), end(v)
#define inputArr(i, arr) \
for (int &i : arr)   \
cin >> i;
#define ll long long
#define ull unsigned long long
#define lld long double
#define kickPrint(tt) cout << "Case #" << tt << ": "

typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> pbds;

time_t Begin;

//////////////////Debug///////////////
#define debug(x)       \
cout << #x << " "; \
_print(x);         \
cout << endl;
void _print(ll t)
{
cout << t;
}
//void _print(int t) {cout << t;}
void _print(string t) { cout << t; }
void _print(char t) { cout << t; }
void _print(lld t) { cout << t; }
void _print(double t) { cout << t; }
void _print(ull t) { cout << t; }
void display(ll a[], ll n)
{
for (ll i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}

template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p)
{
cout << "{";
_print(p.ff);
cout << ",";
_print(p.ss);
cout << "}";
}
template <class T>
void _print(vector<T> v)
{
cout << "[ ";
for (T i : v)
{
_print(i);
cout << " ";
}
cout << "]";
}
template <class T>
void _print(set<T> v)
{
cout << "[ ";
for (T i : v)
{
_print(i);
cout << " ";
}
cout << "]";
}
template <class T>
void _print(multiset<T> v)
{
cout << "[ ";
for (T i : v)
{
_print(i);
cout << " ";
}
cout << "]";
}
template <class T, class V>
void _print(map<T, V> v)
{
cout << "[ ";
for (auto i : v)
{
_print(i);
cout << " ";
}
cout << "]";
}
template <typename T, typename U>
inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }

void init()
{
Begin = clock();
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void timeTaken()
{
#ifndef ONLINE_JUDGE
double time_taken = double(clock() - Begin) / double(CLOCKS_PER_SEC);
cout << "Execution Time: " << fixed << setprecision(5) << time_taken << "s\n";
#endif
}

vector<int> computeLps(string s, int M)
{
int len = 0;
vector<int> lps(M + 20);

lps[0] = 0;

int i = 1;
while (i < M)
{
if (s[i] == s[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
// debug(len);
return lps;
}

int ceiling(int x, int y)
{
int res = x / y;
if (x % y)
{
if (x >= 0)
res++;
}
return res;
}

vector<vector<int>> makePrefix(vector<vector<int>> &grid)
{
int n = grid.size(), m = grid[0].size();
vector<vector<int>> prefix(n + 1, vector<int>(m + 1));

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
prefix[i + 1][j + 1] = grid[i][j] + prefix[i][j + 1] + prefix[i + 1][j] - prefix[i][j];
}
}

return prefix;
}

int query(int x1, int y1, int x2, int y2, vector<vector<int>> &prefix)
{
// cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;

// cout << "query: " << prefix[x2 + 1][y2 + 1] << " " << prefix[x2 + 1][y1] << " " << prefix[x1][y2 + 1] << " " << prefix[x1][y2] << endl;
return prefix[x2 + 1][y2 + 1] - prefix[x2 + 1][y1] - prefix[x1][y2 + 1] + prefix[x1][y1];
}

int toInt(string &s)
{
int res = 0;
for (char c : s)
res = res * 10 + (c - '0');
return res;
}

bool isValid(int i, int j, int n, int m)
{
return i >= 0 && i < n && j >= 0 && j < m;
}

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};

int solveDP(int i, bool bound, int n, int x, vector<vector<int>> &dp)
{
if (i < 0)
return 1;

if (dp[i][bound] != -1)
return dp[i][bound];
int res = 0;

int bit = ((n - 1) & (1 << i)) ? 1 : 0;
int bitN = ((n) & (1 << i)) ? 1 : 0;
if (x & (1 << i))
{
if (bound && bit == 0 && bitN == 1)
res = 0;
else
res = solveDP(i - 1, bound && bit == bitN, n, x, dp);
}
else
{
res = solveDP(i - 1, bound && bit == 0, n, x, dp);
if (!bound || (bit))
res += solveDP(i - 1, bound && bit == 1, n, x, dp);
}

return dp[i][bound] = res;
}

int32_t main()
{
init();
//--------------------
int t = 1;
cin >> t;
// int aMax = 2e6;
// vector<int> fac = calcFactorial(aMax), facInverse = factorialInverseUnderMod(aMax);

for (int tt = 1; tt <= t; tt++)
{
int n, x;
cin >> n >> x;
vector<vector<int>> dp(35, vector<int>(3, -1));
cout << solveDP(31, true, n, x, dp) << endl;
}
//---------------------------
timeTaken();
return 0;
}``````

## Nil Xor CodeChef Solution in PYTH 3

``````t=int(input())
for _ in range(t):
n,x=map(int,input().split())
ans=p=0
for i in range(0,30):
if not x&(1<<i):
if n&(1<<i):ans+=(1<<p)
p+=1
print(ans)``````

## Nil Xor CodeChef Solution in C

``````#include<stdio.h>

#define ll long long

long long dpumber[55][2];
long long number, xumber;
long long dfsumber(int indumber, int gumber) {
if(indumber < 0)
return 1;
if(dpumber[indumber][gumber] != -1)
return dpumber[indumber][gumber];

long long ansumber = 0;
if(xumber >> indumber & 1) {
ansumber += dfsumber(indumber - 1, gumber);
} else {
if(number >> indumber & 1) {
ansumber += dfsumber(indumber - 1, gumber);
ansumber += dfsumber(indumber - 1, 0);
} else {
ansumber += dfsumber(indumber - 1, gumber);
if(gumber == 0) ansumber += dfsumber(indumber - 1, gumber);
}
}

return dpumber[indumber][gumber] = ansumber;
}
int main() {
int tumber;
scanf("%d",&tumber);
while(tumber--) {
memset(dpumber, -1, sizeof (dpumber));
scanf("%d%d",&number,&xumber);
printf("%d\n",dfsumber(39, 1) - 1);
}
}``````

## Nil Xor CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0;i<t;i++){
int n = in.nextInt();
int x = in.nextInt();
int n1[] = new int[32];
int x1[] = new int[32];
for(int j = 31;j>=0;j--){
int rem1 =  n%2;
n1[j] =  rem1;
n = n/2;
int rem2 = x%2;
x1[j] = rem2;
x = x/2;
}
int find = 0;
int c1 = 1;
int c0 = 0;
for(int j = 0;j<32;j++){
if(n1[j]==1&&find==0){
find = j;
}
else if(n1[j]==1){
c1++;
}
else{

}
if(find!=0&&x1[j]==0){
c0++;
}
}
//System.out.println(c1+" "+c0);
c0 = c0-1;
long result = 0;
for(int j = find;j<32;j++){
if(n1[j]==1&&x1[j]==0){
result = result+ (long) Math.pow(2,c0);
c0 = c0-1;
}
else if(n1[j]==0&&x1[j]==0){
c0 = c0-1;
}
}
System.out.println(result);

}
}
}``````

## Nil Xor CodeChef Solution in PYPY 3

``````import sys
from math import *
from collections import *
out=sys.stdout.write
# n=int(inp())
# arr=list(map(int,inp().split()))
for _ in range(int(inp())):
n,x=map(int,inp().split())
l_bit=int(log(n,2))
arr=[0]*(l_bit+1)
for idx in range(l_bit,-1,-1):
if (n & (1<<idx)): arr[idx]=1
memo=[[-1]*2 for _ in range(l_bit+1)]
def dp(idx,restricted):
if idx==0:
if (x & (1<<idx))==0:
if not restricted: return 2
else: return 1 if arr[idx]==1 else 0
else:
if not restricted: return 1
else: return 0
if memo[idx][restricted]!=-1: return memo[idx][restricted]
if (x & (1<<idx))==0:
if not restricted: ans=2*dp(idx-1,restricted)
elif arr[idx]==1: ans=dp(idx-1,restricted)+dp(idx-1,restricted ^ 1)
else: ans=dp(idx-1,restricted)
else:
ans=dp(idx-1,restricted)
memo[idx][restricted]=ans
return ans
print(dp(l_bit,1))

``````

## Nil Xor CodeChef Solution in GO

``````package main

import (
"bufio"
"bytes"
"fmt"
"os"
)

func main() {

var buf bytes.Buffer
for tc > 0 {
tc--
var n, x uint64
res := solve(n, x)
buf.WriteString(fmt.Sprintf("%d\n", res))
}
fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
i := from

var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp

return i
}

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

for i := 0; i < len(s); i++ {
if s[i] == '\n' || s[i] == '\r' {
return s[:i]
}
}
return s
}

return
}

a, b = res[0], res[1]
return
}

a, b, c = res[0], res[1], res[2]
return
}

res := make([]int, n)
x := 0
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
}
return res
}

const H = 30

func solve(n, x uint64) int64 {

tmp := int64(x)

var cnt int

for tmp > 0 {
cnt++
tmp -= tmp & -tmp
}

var res int64

for i := H; i >= 0; i-- {
// x(i) = 1, then k(i)  must be 1, when n(i) is 0, k(i) = 0, when n(i) is 1
// so when x(i) = 0,
if (x>>uint(i))&1 == 0 {
// and we can make k less than n
if (n>>uint(i))&1 == 1 {
res += 1 << uint(i-cnt)
}
} else {
cnt--
}
}

return res
}

func pow(a, b int) int64 {
A := int64(a)
var res int64 = 1
for b > 0 {
if b&1 == 1 {
res *= A
}
A *= A
b >>= 1
}
return res
}``````
##### Nil Xor CodeChef Solution Review:

In our experience, we suggest you solve this Nil Xor CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Nil Xor CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Nil Xor CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!