D. Bits Reverse 2018CCPC桂林站

题目描述

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.

 

【题意】

T组输入,每组输入包含两个数字x和y(long long int)。对x有一种操作,可以将 x的二进制 任意相邻的三个位逆置。

问最少进行多少次操作,可以使得x等于y

【分析】

首先将x和y分别转为二进制并存在数组里。

任意相邻三个位逆置,其实不影响中间那一位,也就是隔位交换。所以奇数位和偶数位分别考虑即可。

对于奇数位,x和y的1的个数若不相等,则无解;否则,设x中1的位置列表ai,y中1的位置列表bi。则

偶数位同理,ans加在一起即为答案。
 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<set>
#pragma warning(disable:4996)
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
typedef long long ll;
ll x, y;
int a[maxn], b[maxn];
int v[maxn], u[maxn];
int main() {
	int cas = 1;
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%lld%lld", &x, &y);
		printf("Case %d: ", cas++);
		for (int i = 0; i < 63; i++) {
			a[i] = (1ll << i) & x;
			b[i] = (1ll << i) & y;
		}
		int cnt1 = 0;
		int cnt2 = 0;
		for (int i = 0; i < 63; i += 2) {
			if (a[i]) {
				u[cnt1++] = i;
			}
			if (b[i]) {
				v[cnt2++] = i;
			}
		}
		if (cnt1 != cnt2) {
			printf("-1\n");
			continue;
		}
		ll ans = 0;
		for (int i = 0; i < cnt1; i++) {
			ans += abs(u[i] - v[i]) / 2;
		}
		 cnt1 = 0;
		 cnt2 = 0;
		for (int i = 1; i <= 63; i += 2) {
			if (a[i]) {
				u[cnt1++] = i;
			}
			if (b[i]) {
				v[cnt2++] = i;
			}
		}
		if (cnt1 != cnt2) {
			printf("-1\n");
			continue;
		}
		for (int i = 0; i < cnt1; i++) {
			ans += abs(u[i] - v[i]) / 2;
		}
		printf("%lld\n", ans);
	}
	return 0;
}

 

### Python `x.reverse` 方法的使用说明与示例 在 Python 中,`x.reverse` 是一种针对列表的操作方法,用于对列表中的元素进行原地反转(即将列表的第一个元素变为最后一个,第二个变为倒数第二个,依此类推)。此方法不会返回任何值(返回值为 `None`),但它会直接修改原始列表。 #### 基本语法 ```python x.reverse() ``` - **功能描述**:该方法将列表中的元素顺序完全颠倒。 - **适用范围**:仅适用于列表类型的数据结构。对于其他不可变数据类型(如字符串、元组等),不支持这一方法[^1]。 --- #### 示例一:基本用法——列表反转 以下代码展示了如何通过 `reverse()` 对整数列表进行反转: ```python my_list = [1, 2, 3, 4, 5] my_list.reverse() print(my_list) # 输出: [5, 4, 3, 2, 1] ``` --- #### 示例二:嵌套列表的反转 需要注意的是,`reverse()` 只会对顶层列表的元素顺序进行反转,而不会影响子列表内部的内容: ```python nested_list = [[1, 2], [3, 4], [5, 6]] nested_list.reverse() print(nested_list) # 输出: [[5, 6], [3, 4], [1, 2]] ``` 尽管外部列表被反转了,但 `[1, 2]`, `[3, 4]`, `[5, 6]` 这些子列表本身并未改变其内部顺序。 --- #### 示例三:与其他方法结合使用 可以将 `reverse()` 结合其他方法一起使用,比如先排序再反转以获得降序效果: ```python numbers = [7, 3, 9, 1, 5] numbers.sort() # 升序排序 numbers.reverse() # 排序后再反转得到降序 print(numbers) # 输出: [9, 7, 5, 3, 1] ``` 这种方法虽然有效,但在某些情况下可能不如直接使用 `sort(reverse=True)` 更加简洁明了[^1]。 --- #### 特殊情况处理 如果尝试对空列表调用 `reverse()` 方法,则不会引发错误,也不会有任何变化: ```python empty_list = [] empty_list.reverse() print(empty_list) # 输出: [] ``` 另外值得注意的一点是,由于 `reverse()` 修改的是原对象而非创建新副本,所以在需要保持原有数据不变的情况下应该谨慎使用。 --- ### 总结 `x.reverse` 提供了一种简单直观的方式来快速翻转列表中所有项的位置关系。然而要注意它的局限性在于只能作用于可变序列类型之上,并且执行过程属于就地更新性质而不是生成新的实例。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值