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

提示

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.

题意

把二进制的a 每次翻转其中三位的位置使其转换成二进制的b 需要几步操作

题解

先转换成二进制这是肯定的把然后把位数对齐(这很重要)

因为每次翻转三位,所以实际上只有第一个位置和第三个位置做了调换。从后往前枚举a和b数不同的位置i,然后往前不断隔2个位置找,找到能够满足与i状态相反的的位置j,将这两个位置调换。调换需要的操作次数就是(i - j) / 2;

代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define scac(x) scanf("%c",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pri3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prl(x) printf("%lld\n",x)
#define prl2(x,y) printf("%lld %lld\n",x,y)
#define prl3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define mst(x,y) memset(x,y,sizeof(x))
#define ll long long
#define LL long long
#define pb push_back
#define mp make_pair
#define P pair<double,double>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define mod 1e9+7
#define INF 0x3f3f3f3f
#define N 1005
const int maxn = 1000005;
ll x,y;
string s1,s2;
int cnt1,cnt2;
int kase;
int main()
{
int t;
sca(t);
kase = 0;
while(t--)
{
s1.clear();
s2.clear();
scl2(x,y);
cnt1=cnt2=0;
while(x)
{
if(x%2 == 1)
{
s1 += "1";
cnt1++;
}
else
s1+="0";
x/=2;
}
while(y)
{
if(y%2 == 1)
{
s2 += "1";
cnt2++;
}
else
s2+="0";
y/=2;
}
printf("Case %d: ",++kase);
if(cnt1 != cnt2 )
{
pri(-1);
continue;
}
int len = max(s1.length(),s2.length());
if(s1.length()<len)
{
int j = len - s1.length();
rep(i,0,j)
s1+='0';
}
else if(s2.length()<len)
{
int j = len - s2.length();
rep(i,0,j)
s2+='0';
}
ll ans = 0;
int fl = 1;
for(int i = len-1; i >= 0; i--)
{
if(s2[i] == '0' && s1[i] == '1')
{
for(int j = i; j >=0;j-=2)
{
if(s1[j] == '0' && s2[j] == '1')
{
s2[j] = '0';
ans += (i-j)/2;
break;
}
}
}
else if(s1[i] == '0' && s2[i] == '1')
{
for(int j = i; j >= 0;j -= 2)
{
if(s1[j] == '1' && s2[j] == '0')
{
s1[j] = '0';
ans += (i-j)/2;
break;
}
}
}
}
prl(ans);
}
return 0;
}

upc组队赛17 Bits Reverse【暴力枚举】的更多相关文章

  1. upc组队赛2 Super-palindrome【暴力枚举】

    Super-palindrome 题目描述 You are given a string that is consisted of lowercase English alphabet. You ar ...

  2. upc组队赛12 Cardboard Container【枚举】

    Cardboard Container Problem Description fidget spinners are so 2017; this years' rage are fidget cub ...

  3. upc组队赛17 Stone Game【极小值】

    Stone Game 题目链接 题目描述 Alice and Bob are always playing game! The game today is about taking out stone ...

  4. upc组队赛17 Greatest Common Divisor【gcd+最小质因数】

    Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...

  5. upc组队赛6 Odd Gnome【枚举】

    Odd Gnome 题目描述 According to the legend of Wizardry and Witchcraft, gnomes live in burrows undergroun ...

  6. upc组队赛5 Ground Defense【枚举】

    Ground Defense 题目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced ...

  7. HDU 1015.Safecracker【暴力枚举】【8月17】

    Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===  "The item is lo ...

  8. HDU 6351暴力枚举 6354计算几何

    Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  9. hihoCoder #1179 : 永恒游戏 (暴力枚举)

    题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...

随机推荐

  1. 三连击 P1008 洛谷 python写法

    三连击 P1008 洛谷 题意 将\(1,2, \cdots,9\)共9个数分成3组,分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 解题思路 这里我使用了 ...

  2. WOJ#1243 蜥蜴 lizard

    描述 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任 ...

  3. java 标识接口的作用

    标识接口的作用 标识接口是没有任何方法和属性的接口.标识接口不对实现它的类有任何语义上的要求,它仅仅表明实现它的类属于一个特定的类型. 标接口在Java语言中有一些很著名的应用,例如我们常用的Arra ...

  4. redis关闭和启动

    redis关闭 到redis节点目录下执行如下命令 redis-cli -p 端口号 shutdown redis启动 ./redis-server 参数 参数:redis.conf文件全路径 需要到 ...

  5. 3183 RMQ / 贪心(坑成。。)

    题意:删去m个数,使剩下的数组成的数最小 题解 :贪心 , RMQ RMQ解法,建st表找,用rmq找最小值的下标,注意点 ,因为最小值是区间最右最小值,所以应该改成 <= 而不是< mi ...

  6. maven整合S2SH

    1.pom.xml <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.ap ...

  7. 解决pycharm运行py文件时只有unittest选项的方法

    有时候在编完脚本开始运行时,发现某个py脚本右键运行的选项不是run,二是run in unittest,试过很多方法都不能很好的去除,主要是因为脚本中含有test字符串,一种解决方法是将脚本中所有的 ...

  8. Swagger详解(SpringBoot+Swagger集成)(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ai_miracle/article/de ...

  9. spring requestbody json

    1  @requestbody string param 前台将jsonobject序列化成字符串 后台解析成JsonObject 2 @requestbody map<string,objec ...

  10. P4206[NOI2005]聪聪与可可

    链接P4206 [NOI2005]聪聪与可可 类似于开车旅行,如果老鼠确定了那么猫的路线是确定的. 预处理\(g_{i,j}\)表示老鼠在\(i\)号点,猫的下一步方向,\(Bfs\)就行了 设\(f ...