题目 | AND and SUM
AtCoder Beginner Contest 238
D - AND and SUM
https://atcoder.jp/contests/abc238/tasks/abc238_d
Time Limit: 2 sec / Memory Limit: 1024 MB
Score :
Problem Statement
Solve the following problem for
Given are non-negative integers
and . Is there a pair of non-negative integers that satisfies both of the conditions below?
Constraints
- All values in input are integers.
Input
Input is given from Standard Input. The first line is in the following format:
Then,
Output
Print Yes
if, in the No
otherwise.
Sample Input 1
2
1 8
4 2
Sample Output 1
Yes
No
In the first test case, some pairs such as
In the second test case, no pair of non-negative integers satisfies the conditions.
Sample Input 2
4
201408139683277485 381410962404666524
360288799186493714 788806911317182736
18999951915747344 451273909320288229
962424162689761932 1097438793187620758
Sample Output 2
No
Yes
Yes
No
我的笔记
本题纯思维题,需要两个结论:
下面来推导一下这两个结论。
先看第一个结论:
用具体的数来说明更形象,不妨令:
那么:
观察上面两数,用通俗的话来解释:
因此这两个事件不可能同时发生,即不可能两个数的同一位既同时为
再看第二个结论:
我们把
再将
至于为什么另外两个数加起来是
本题解法:
本题知道:
由第二个结论可求出
再使用第一个结论,若
还有一个大前提,
源码
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int T;
cin >> T;
while (T--)
{
long long a, s;
cin >> a >> s;
long long x = s - 2 * a;
if (x >= 0 && !(x & a))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi