//x^(x-1)=2lowbit(x)-1,x^(x-1)^2x^2(x-1)=2lowbit(x)+1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,w,o,e,r,ans;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n,ans=0;
w=n-1,o=0;
for(ll i=1ll<<59,j=59;i;i>>=1,--j) e=(w&(i-1))%3,e&&!(j&1)&&(e^=3),r=((w>>j)-e+2)/3,r<0&&(r=0),((r^o)&1)&&(ans^=i<<1|1),o=r;
ans^=n,!(n%3)&&(ans^=1);
cout<<ans<<"\n";
}
return 0;
}
:::
:::info[推了最后一段的 AC 记录]
AC Code:
//x^(x-1)=2lowbit(x)-1,x^(x-1)^2x^2(x-1)=2lowbit(x)+1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll n,w,o,e,r,ans;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n,ans=0;
w=n-1,o=0;
for(ll i=1ll<<59,j=59;i;i>>=1,--j) e=(w&(i-1))%3,e&&!(j&1)&&(e^=3),r=((w>>j)-e+2)/3,r&1&&(ans^=i<<1|i);
ans^=n,!(n%3)&&(ans^=1);
cout<<ans<<"\n";
}
return 0;
}
:::
然后推最后一段的最大用处是 Code Golf(
代码长度 176B,目前最短解。
#import<ios>
long n,e,A,i,j;main(){while(~scanf("%ld",&n))if(j--){for(j=60;j--;)i=1l<<j,e=(n-1&i-1)%3,!e|j&1||(e^=3),A^=(((n-1)/i-e+2)/3&1)*i*3;A=!printf("%ld\n",A^n^!(n%3));}}