# Build a tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 937    Accepted Submission(s): 366

Problem Description
HazelFan wants to build a rooted tree. The tree has n nodes
labeled 0 to n−1,
and the father of the node labeled i is
the node labeled ⌊i−1k⌋.
HazelFan wonders the size of every subtree, and you just need to tell him the XOR value of these answers.

Input
The first line contains a positive integer T(1≤T≤5),
denoting the number of test cases.
For each test case:
A single line contains two positive integers n,k(1≤n,k≤1018).

Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.

Sample Input
2
5 2
5 3

Sample Output
7
6

``````int get_ans(int n)
{
int ans=n;
for(int i=1;i<n;i  )
ans^=i;
return ans;
}
int main()
{
for(int i=1;i<=48;i  )
{
cout<<get_ans(i)<<' ';
if(i%4==0)cout<<endl;
}
return 0;
}``````

``````1 3 0 4
1 7 0 8
1 11 0 12
1 15 0 16
1 19 0 20
1 23 0 24
1 27 0 28
1 31 0 32
1 35 0 36
1 39 0 40
1 43 0 44
1 47 0 48 ``````

``````if(k==1)
{
if(n%4==1)cout<<"1"<<endl;
else if(n%4==2)cout<<n 1<<endl;
else if(n%4==3)cout<<"0"<<endl;
else cout<<n<<endl;
}``````

``````#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#include <stdlib.h>
#define lowbit(x) (x&-x)
#define e exp(1.0)
const int mod=1e9 7;
const int inf=0x3f3f3f;
//ios::sync_with_stdio(false);
//    auto start = clock();
//    cout << (clock() - start) / (double)CLOCKS_PER_SEC;
typedef long long ll;
typedef long long LL;
using namespace std;
//int get_ans(int n)
//{
//  int ans=n;
//for(int i=1;i<n;i  )
//  ans^=i;
//return ans;
//}
int main()
{
ios::sync_with_stdio(false);
//for(int i=1;i<=48;i  )//打表求1-n的異或
//{
// cout<<get_ans(i)<<' ';
// if(i%4==0)cout<<endl;
//}
int T;
cin>>T;
while(T--)
{
ll n,k;
cin>>n>>k;
if(k==1)
{
if(n%4==1)cout<<"1"<<endl;
else if(n%4==2)cout<<n 1<<endl;
else if(n%4==3)cout<<"0"<<endl;
else cout<<n<<endl;
continue;
}
ll ans=n;
while(1)
{
if(n-1<=k)//depth＝1
{
if(n%2==0)// xor一個數兩次等於不做任何處理
ans^=1;
break;
}
ll x=1,now=1,full=1;
while(x>=0 && now>=0 && now k*x<n)
{
x*=k;
now =x;
full^=now;
}
ll l=(n-now-1)/x;//不滿足k叉樹的左邊樹的數量
ll r=k-l-1;
if(k%2==1)
{
if(l%2==1)ans^=full;
if(r%2==1)ans^=(full^now);
}
else
{
if(l%2==1)ans^=now;
if(r%2==1)ans^=(now-x);
}
n-=(l*now r*(now-x) 1);
ans^=n;
}
cout<<ans<<endl;
}
return 0;
}``````