问题 D: Bits Reverse

博客围绕一个二进制位颠倒问题展开,给定两个整数x和y,可花费一枚硬币颠倒任意数二进制形式中连续三位的顺序,求使x等于y的最少硬币数。题解是将x和y转为二进制存于数组,分别考虑奇数位和偶数位,若对应位1的个数不等则无解,否则计算不同数字个数,进而得出答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接
题目描述

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.

题解:

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

任意相邻三个位颠倒顺序,其实不影响中间那一位,即隔位交换。奇数位和偶数位分别考虑即可。

对于奇数位,x和y的1的个数若不相等,则无解;否则,记录x的奇数位和y的奇数位数字不同的个数 ans1

对于偶数位,x和y的1的个数若不相等,则无解;否则,记录x的偶数位和y的偶数位数字不同的个数 ans2

ans = ans1 + ans2 >> 1 即为答案

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t,x,y,na,nb,cur1,cur2,ans,ans1,ans2,a[105],b[105];
int main(){
    scanf("%lld",&t);
    for(int i = 1;i <= t;i++){
        printf("Case %d: ",i);
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        scanf("%lld%lld",&x,&y);
        cur1 = cur2 = 1;
        while(x){
        	a[cur1++] = x & 1;
       		x >>= 1;
    	}
    	while(y){
        	b[cur2++] = y & 1;
       		y >>= 1;
    	}
        na = nb = ans = ans1 = ans2 = 0;
        for(int i = 1;i <= 100;i += 2){ // 判断奇数位 
            if(a[i] == 1) na++;
            if(b[i] == 1) nb++;
            if(a[i] != b[i]) ans1++;
        }
        if(na != nb){
            printf("-1\n");
            continue;
        }
        na = nb = 0; 
        for(int i = 2;i <= 100;i += 2){ // 判断偶数位 
            if(a[i] == 1) na++;
            if(b[i] == 1) nb++;
            if(a[i] != b[i]) ans2++;
        }
        if(na != nb){
            printf("-1\n");
            continue;
        }
        printf("%d\n",ans1 + ans2 >> 1);
    }
    return 0;
}
编译错误. 5123.cpp: In function 'std::vector<std::basic_string<char> > dijkstra(const std::vector<std::vector<int> >&, const std::vector<std::basic_string<char> >&, const string&, const string&)': 5123.cpp:19:67: error: wrong number of template arguments (0, should be 1) priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; ^ In file included from /usr/include/c++/4.8.2/string:48:0, from /usr/include/c++/4.8.2/bits/locale_classes.h:40, from /usr/include/c++/4.8.2/bits/ios_base.h:41, from /usr/include/c++/4.8.2/ios:42, from /usr/include/c++/4.8.2/ostream:38, from /usr/include/c++/4.8.2/iostream:39, from 5123.cpp:1: /usr/include/c++/4.8.2/bits/stl_function.h:222:12: error: provided for 'template<class _Tp> struct std::greater' struct greater : public binary_function<_Tp, _Tp, bool> ^ 5123.cpp:19:68: error: template argument 3 is invalid priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; ^ 5123.cpp:19:73: error: invalid type in declaration before ';' token priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; ^ 5123.cpp:23:8: error: request for member 'emplace' in 'pq', which is of non-class type 'int' pq.emplace(0, src_idx); ^ 5123.cpp:25:16: error: request for member 'empty' in 'pq', which is of non-class type 'int' while (!pq.empty()) { ^ 5123.cpp:26:14: error: expected unqualified-id before '[' token auto [d, u] = pq.top(); ^ 5123.cpp:27:12: error: request for member 'pop' in 'pq', which is of non-class type 'int' pq.pop(); ^ 5123.cpp:28:13: error: 'd' was not declared in this scope if (d > dist[u]) continue; ^ 5123.cpp:28:22: error: 'u' was not declared in this scope if (d > dist[u]) continue; ^ 5123.cpp:31:23: error: 'u' was not declared in this scope if (graph[u][v] != INT_MAX && d + graph[u][v] < dist[v]) { ^ 5123.cpp:31:43: error: 'd' was not declared in this scope if (graph[u][v] != INT_MAX && d + graph[u][v] < dist[v]) { ^ 5123.cpp:34:20: error: request for member 'emplace' in 'pq', which is of non-class type 'int' pq.emplace(dist[v], v); ^ 5123.cpp:50:37: error: 'reverse' was not declared in this scope reverse(path.begin(), path.end()); ^ 5123.cpp: In function
最新发布
05-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值