Codeforces 第 976 轮(第 2 单元)(A、B、C、D、E)并发检查集、区间合并、概率 dp-C 问题
最编程
2024-10-03 15:44:37
...
思路
所以可以从高位到低位贪心地确定 a a a的该位取什么值,然后分类讨论即可。
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
int b, c, d;
void solve()
{
cin >> b >> c >> d;
int ans = 0;
bool ok = true;
for (int j = 61; j >= 0; j--)
{
int bitb = (b >> j) & 1;
int bitc = (c >> j) & 1;
int bitd = (d >> j) & 1;
if (bitd == 1)
{
if (bitb == 1 && bitc == 1)continue;
if (bitb == 1 && bitc == 0)continue;
if (bitb == 0 && bitc == 1) {ok = false; continue;}
if (bitb == 0 && bitc == 0)ans += (1ll << j);
}
else
{
if (bitb == 1 && bitc == 1) {ans += (1ll << j); continue;}
if (bitb == 1 && bitc == 0) {ok = false; continue;}
if (bitb == 0 && bitc == 1) {ans += (1ll << j); continue;}
if (bitb == 0 && bitc == 0)continue;
}
}
if (!ok)
{
cout << -1 << endl;
}
else
{
cout << ans << endl;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int test = 1;
cin >> test;
for (int i = 1; i <= test; i++)
{
solve();
}
return 0;
}
上一篇: MySQL 表的添加、删除和更改(基础)
下一篇: 接口 抽象类