CCPC2018 桂林 D "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.


输出
Please output T lines exactly.
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
样例输入输出

题意:

  给你两个数 x,y,定义一个操作,可以反转连续的三个位置(转化成二进制后的连续三个位置);

  问,最少需要多少操作,可以使得 x == y;

  如果不能,输出 -1;

思路:

  这题卡了一会,如何快速交换二进制的两个位的值呢?

  答案:异或大法好;

  假设 x = (100)

  定义 t1 = x&1 , t3 = x>>2&1 ;

  x ^= (t1^t3);

  x ^= (t1^t3)<<2;

本来就不是难题,还是,数据不太给力????????

为啥看了几篇2018CCPC桂林游记,他们的这个题,都wa了好多发呢????

总有种不太靠谱的感觉,可,也找不出错误样例了!!!!

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 
 5 ll x,y;
 6 
 7 int Solve()
 8 {
 9     if(x > y)
10         swap(x,y);
11 
12     int ans=0;
13     while(x != y)
14     {
15         if((x&1) == (y&1))
16         {
17             x >>= 1;
18             y >>= 1;
19             continue;
20         }
21         ///交换t1,t3
22         int t1=x&1;
23         int t3=(x>>2)&1;
24         x ^= (t1^t3);
25         x ^= (t1^t3)<<2;
26         ans++;
27         if((x&1) != (y&1))
28             return -1;
29     }
30     return ans;
31 }
32 int main()
33 {
34     int test;
35     scanf("%d",&test);
36     for(int kase=1;kase <= test;++kase)
37     {
38         scanf("%lld%lld",&x,&y);
39         printf("Case %d: %d\n",kase,Solve());
40     }
41     return 0;
42 }
View Code

猜你喜欢

转载自www.cnblogs.com/violet-acmer/p/10803124.html