Vlad and a Pair of Numbers
题目描述:
Vlad found two positive numbers
Since it is easier to remember one number than two, Vlad remembered only
输入描述:
The first line of the input data contains the single integer
Each test case is described by a single integer
输出描述:
Output
. If there are several answers, output any of them. If there are no matching pairs, output
样例:
input:
6
2
5
10
6
18
36
output:
3 1
-1
13 7
-1
25 11
50 22
AC代码:
#include <bits/stdc++.h> using namespace std; typedef long long LL; // 对于一个数x,某个位上是1的情况下 // 让a在对应位上为1,b在对应位上为0 // 而对于x某个位上是0的情况下 // 先贪心地让a,b对应位都为1 // 最后判断一下是否符合题意即可 void solve() { int x; cin >> x; // a和b必是一个大于x,一个小于x int a = x; int b = 0; for(int i = 29; i >= 0; i --) { // 防止前面有0的情况,比如10 = (000...00001010) if(x & (1 << i) > 0) continue; // 在某个位是0的情况下,假设a,b对应位都为1,所以就加上两倍的(1 << i) // 只要小于等于2 * x就可以 if(2 * x >= (2 << i) + a + b) { a += (1 << i); b += (1 << i); } } if(a + b == 2 * x && (a ^ b) == x) cout << a << ' ' << b << '\n'; else cout << -1 << '\n'; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T; cin >> T; while(T --) solve(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】