In this HackerEarth The Castle Gate problem solution Gudi, a fun-loving girl from the city of Dun, travels to Azkahar - a strange land beyond the mountains. She arrives at the gates of Castle Grey, owned by Puchi, the lord of Azkahar to claim the treasure that it guards. However, destiny has other plans for her as she has to move through floors, crossing obstacles on her way to reach the treasure.

The gates of the castle are closed. An integer N is engraved on the gates. Writing on the wall says "Tap the gates as many times as there are unordered pairs of distinct integers from 1 to N whose bit-wise XOR does not exceed N". Help her find the number of the times she has to tap.



HackerEarth The Castle Gate problem solution


HackerEarth The Castle Gate problem solution.

#include<bits/stdc++.h>


using namespace std;

#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define elif else if
#define pii pair<int,int>
#define mp make_pair
#define pb push_back


int main()
{
  ios_base::sync_with_stdio(0);
  int t;
  cin>>t;
  assert(1<=t && t<=100);
  while(t--)
  {
    int n,i,j,ans=0;
    cin>>n;
    assert(2<=n && n<=2000);
    for(i=1 ; i<=n ; i++)
    {
      for( j=i+1; j<=n; j++ )
      {
        if( (i^j) <= n)
        {
          ans++;
        }
      }
    }
    cout<<ans;
    if(t>0)
      cout<<endl;
  }
 return 0;
}

Second solution

from itertools import product
print "\n".join([(lambda n: str(len([1 for i,j in product(range(n),repeat=2) if i<j and (i+1)^(j+1) <= n])))(int(raw_input())) for _ in xrange(int(raw_input()))])