跳到正文

题解

题解:CF1790E Vlad and a Pair of Numbers

hwq202314约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年10月3日
收录
2026年10月3日
更新
2026年11月11日
篇幅
约 5 分钟
题目
CF1790E

版本

  1. v2当前
  2. v1

由于正解做法题解区太多了,我就不写正常做法的题解了。说一下模拟赛赛时做法。

题目要求构造满足 x=a⊕b=a+b2x = a \oplus b = \frac{a + b}{2} 的一组 (a,b)(a,b)。

这对我解题不重要,直接来观察样例 22:

这里我们可以发现有解的时候输出都是 (x2,3x2)(\frac{x}{2},\frac{3x}{2}),经过验证,我猜测这一定是个通解,那输出方案就有了。

那什么时候无解呢?我们观察一下 checker.cpp。

checker.cpp
C++
#include "testlib.h"
#include <climits>
using namespace std;
bool hasSolution(long long x){
    if(x<=0) return false;
    if(x&1LL) return false;
    if(x&(x>>1)) return false;
    return true;
}
int main(int argc,char* argv[]){
    registerTestlibCmd(argc, argv);
    int T=inf.readInt();
    for(int tc=1;tc<=T;++tc){
        long long x=inf.readLong();
        long long a=ouf.readLong();
        if(a==-1){
            if(hasSolution(x))
                quitf(_wa,"Test case %d: x = %lld has solution, but output -1",tc,x);
        }
        else{
            if(a==0)
                quitf(_wa,"Test case %d: a must be at least 1",tc);
            long long b=ouf.readLong();
            if((__int128)a+b!=(__int128)2*x)
                quitf(_wa,"Test case %d: a + b != 2 * x",tc);
            if((a^b)!=x)
                quitf(_wa,"Test case %d: a xor b != x",tc);
        }
    }
    quitf(_ok,"All answers are correct.");
}

看完这个 checker.cpp 后,我想大家都知道怎么判有无解了吧,bool hasSolution(long long x) 这个函数就是来判有无解的。

那这道题就做完了啊,这样我们连题目都没思考过就做完了。

简单证明一下
{a=(axor⁡b)or⁡ (aand⁡b)=(axor⁡b)+(aand⁡b)=3(axor⁡b)2b=aand⁡b=axor⁡b2\begin{cases} a = (a\operatorname{xor} b) \operatorname{or}\ (a\operatorname{and} b) = (a\operatorname{xor} b) + (a\operatorname{and} b) = \dfrac{3(a\operatorname{xor} b)}{2}\\ b = a\operatorname{and} b = \dfrac{a\operatorname{xor} b}{2} \end{cases}
AC 代码
C++
#include <bits/stdc++.h>

namespace Qwarth {

using i64 = long long;

void main() {
	i64 x;
	std::cin >> x;
	if ((x <= 0LL) || (x & 1LL) || (x & (x >> 1LL))) {
		std::cout << "-1\n";
	} else {
		i64 a = x >> 1LL, b = a * 3;
		if ((a ^ b) != x) {
			std::cout << "-1\n";
		} else {
			std::cout << a << ' ' << b << '\n';
		}
	}
}
}  // namespace Qwarth

int main() {
	freopen("stoat.in", "r", stdin);
	freopen("stoat.out", "w", stdout);
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	int _T = 1;
	std::cin >> _T;
	while (_T--) {
		Qwarth::main();
	}
	return 0;
}
原文