博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
upc组队赛17 Bits Reverse【暴力枚举】
阅读量:6831 次
发布时间:2019-06-26

本文共 3248 字,大约阅读时间需要 10 分钟。

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.

样例输入

30 33 66 9

样例输出

Case 1: -1Case 2: 1Case 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
using namespace std;#define rep(i,a,n) for(int i=a;i
#define PLL pair
#define PI acos(1.0)#define eps 1e-6#define inf 1e17#define mod 1e9+7#define INF 0x3f3f3f3f#define N 1005const 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()
= 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;}

转载于:https://www.cnblogs.com/llke/p/10815631.html

你可能感兴趣的文章
java ee
查看>>
复制文字,链接,剪贴板的使用
查看>>
RSA加解密-2
查看>>
正向与反向代理的理解
查看>>
二分搜索法
查看>>
关于createTextRange和createRange的一些用法【转】
查看>>
关于jquery的serialize方法转换空格为+号的解决方法
查看>>
微信发一个网址打开后自动调用手机自带默认浏览器或提示选择浏览器打开如何实现?...
查看>>
ADO.NET 快速入门(二):执行命令
查看>>
菜鸟学习WPF(一):开篇
查看>>
Hibernate查询HQL(第二部分)
查看>>
数据源配置
查看>>
闲置的2017
查看>>
【习题1】第一个程序【第2天】
查看>>
JavaScript 编程模式
查看>>
c#获取文件夹路径(转载)
查看>>
BZOJ2940 条纹
查看>>
WCF 第五章 行为 事务之事务服务行为
查看>>
转:java内部类作用
查看>>
在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
查看>>