# Bits Reverse

Now given two integers x and y, you can reverse every consecutive three bits in arbitrary number’s binary form (any leading zero can be taken into account) using one coin. Reversing (1,2,3) means changing it into (3,2,1).
Could you please find a way that minimize number of coins so that x = y? If you can, just output the minimum coins you need to use.

The first line of input file contains only one integer T (1≤T≤10000) indicating number of test cases.
Then there are T lines followed, with each line representing one test case.
For each case, there are two integers x, y (0≤x,y≤1018) described above.

For each line, output Case d: (d represents the order of the test case) first. Then output the answer in the same line. If there is no way for that, print -1 instead.

## 样例输入

3
0 3
3 6
6 9


Case 1: -1
Case 2: 1
Case 3: 2


Sample 1: Considering following two binary string:
0: 0 ...0000
3: 0 ...0011
There is no way to achieve the goal.
Sample 2: Considering following two binary string:
3: 0 ...0011
6: 0 ...0110
You can reverse the lowest three digits in 3 so that 3 is changed into 6.
You just need to perform one reverse so that the minimum coin you need to use is 1.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
typedef long long ll;

using namespace std;
const int maxn = 100;
int l1,l2;
int A[100],B[100];
int main()
{
int t;
cin>>t;
int x = 0;
while(t--){
x++;
l1 = 100;
l2 = 100;
ll a,b;
cin>>a>>b;
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
while(a){
A[l1--] = (a&1);
a/=2;
}
while(b){
B[l2--] = (b&1);
b/=2;
}
int res = 0;
int flag,i;
int flag2 = 0;
for(i = 1;i <= 100;i++){
flag = 0;
if(A[i] != B[i]){
for(int j = i;j<=100;j += 2){
if(A[j] == B[i]){
res += (j-i)/2;
A[j] = A[i];
A[i] = B[i];
flag = 1;
break;
}
}
}
if(A[i] != B[i]){
flag2 = 1;
break;
}
}
if(flag2 == 0)
printf("Case %d: %d\n",x,res);
else
printf("Case %d: -1\n",x,res);

}
}


05-25 46

06-18 5103

03-08 200

03-11 108

03-09 1万+

03-27 343

09-25 204

#### reverse_bits

©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。