C. Bitwise Balancing

原题

C. Bitwise Balancing

解析

题意如图

我们要求 a , 其实 a 只要满足条件即可, 我们可以发现每一位都不会影响到其它位, 因此对每一位检查, 发现对b c a, d 有固定结果

b c a d

0 0 0 0

0 0 1 1

0 1 0 0

0 1 1 0

1 0 0 1

1 0 1 1

1 1 0 1

1 1 1 0

bcd 无法为 011 和 100, 其它皆可找到对应的 a

代码

cpp 复制代码
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 200010;

int n, m, k, q, ans;

int a[N];

void solve()
{
	int b, c, d;
	cin >> b >> c >> d;
	
	int mask = 1;
	
	int a = 0, bb = 0, cc = 0, dd = 0;
	
	for (int i = 1; i <= 62; i ++ )
	{
		if (b & mask)
		{
			bb = 1;
		}
		else
		{
			bb = 0;
		}
		if (c & mask)
		{
			cc = 1;
		}
		else
		{
			cc = 0;
		}
		if (d & mask)
		{
			dd = 1;
		}
		else
		{
			dd = 0;
		}
		
		if(bb == 0 && cc == 1 && dd == 1)
		{
			cout << -1 << "\n";
			return;
		}
		
		if(bb == 1 && cc == 0 && dd == 0)
		{
			cout << -1 << "\n";
			return;
		}
		
		if(bb == 1 && cc == 1 && dd == 0)
		{
			a += mask;
		}
		
		if(bb == 0 && cc == 0 && dd == 1)
		{
			a += mask;
		}
		
		mask <<= 1;
	}
	
	cout << a << "\n";
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T = 1;
	cin >> T;
	while (T -- )
	{
		solve();
	}
}
相关推荐
Dxy12393102165 小时前
Python 使用正则表达式将多个空格替换为一个空格
开发语言·python·正则表达式
疏星浅月6 小时前
虚拟内存三大核心作用详解
linux·c语言·arm开发·嵌入式硬件
故事和你916 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
techdashen7 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
星空椰7 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
忒可君7 小时前
C# winform 自制分页功能
android·开发语言·c#
Rust研习社7 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
leaves falling8 小时前
C++模板进阶
开发语言·c++
无敌昊哥战神8 小时前
【保姆级题解】力扣17. 电话号码的字母组合 (回溯算法经典入门) | Python/C/C++多语言详解
c语言·c++·python·算法·leetcode
坐吃山猪9 小时前
Python27_协程游戏理解
开发语言·python·游戏