• + 4 comments

    I am explaining how G function is working.

    Number Binary repr. xor from 1 to n A(1)^A(2)...^A(n)

    • 0 0000 0000 0000=0
    • 1 0001 0001 0001=1
    • 2 0010 0011 0010=2
    • 3 0011 0000 0010=2
    • 4 0100 0100 0110=6
    • 5 0101 0001 0111=7
    • 6 0110 0111 0000=0
    • 7 0111 0000 0000=0

    from above we can deduce that G is periodic function with period of 8 now for a==0 or a==1 value is x for a==2 or a==3 value is 2 for a==4 or a==5 value is x+2 for a==6 or a==7 value is 0 For better undestanding how to calculate xor of 1 to n efficiently please refer this link:-xor from 1 to n here is my solution

    #include<bits/stdc++.h>
    using namespace std;
    long long uptonxor(long long n)
    {
        int a=n%8;
        if(a==0 || a==1)
            return n;
        else if(a==2 || a==3)
            return 2;
        else if(a==4 || a==5)
            return n+2;
        return 0;
    }
    int main()
    {
        int q;
        cin>>q;
        while(q--)
        {
            long long l,r;
            cin>>l>>r;
            long long ans;
            ans=uptonxor(l-1)^uptonxor(r);
            cout<<ans<<endl;
        }
        return 0;
    }