AND and SUM
問題
D – AND and SUM
プログラミング初級者から上級者まで楽しめる、競技プログラミングコンテストサイト「AtCoder」。オンラインで毎週開催プログラミングコンテストを開催しています。競技…
記録
- 2022/2/5 リアルタイム参加。解法わからず。
解法
- \(x \hspace{4pt} AND \hspace{4pt} y = a\)
- \(x + y = s\)
- \(x + y = (x \hspace{4pt} XOR \hspace{4pt} y) + (x \hspace{4pt} AND \hspace{4pt} y) * 2\)
- \(s = (x \hspace{4pt} XOR \hspace{4pt} y) + a * 2\)
- \(s – a * 2 = (x \hspace{4pt} XOR \hspace{4pt} y)\)
- \((x \hspace{4pt} XOR \hspace{4pt} y) \hspace{4pt} AND \hspace{4pt} a = 0\)
- \((s – a * 2) \hspace{4pt} AND \hspace{4pt} a = 0\) なら
Yes
コード例
#include <bits/stdc++.h> using namespace std; #include <atcoder/all> using namespace atcoder; using ll = long long; using P = pair<ll, ll>; #define rep(i, n) for(int i = 0; i < (n); ++i) // using mint = modint998244353; // using mint = modint1000000007; // const int mod = 1000000007; // const ll INF = 1LL << 62; // const int INF = 1001001001; int main() { int T; cin >> T; vector<P> as(T); rep(i, T) { ll a, s; cin >> a >> s; as[i] = make_pair(a, s); } rep(i, T) { ll a = as[i].first; ll s = as[i].second; if(0 <= (s - a * 2) && ((s - a * 2) & a) == 0) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }