题解:P17594 「CZOI-R8」Nailoong and Operations
违规用户名1764483 · · 题解
题目大意
给定两个非负整数
- 对
x 进行这个操作:x\gets x - p 。 - 对
y 进行以下两个操作中的一个操作:-
y\gets y + p -
y\gets y\operatorname{or} p
-
我们需要求所有操作后的
题目解法
我们考虑从低到高按位进行动态规划。
我们定义
设
- 当
x_i = 0 时,我们毫无疑问只能选择从低位进位而来的选择,设进位情况为c ,那么我们可以得到dp_{\lfloor \frac{c + x_i}{2} \rfloor, i + 1} = \min \{dp_{j, i} + 2^i \times ((x_i + c) \bmod 2)\} 。 - 当
x_i = 1 时,我们既可以选择让这一位使用或运算,也可以选择使用加运算,那么我们分情况考虑:- 当我们选择或运算时,当前位置一定是
1 ,所以我们不难得到dp_{\lfloor \frac{c + x_i}{2} \rfloor, i + 1} = \min \{dp_{j, i} + 2^i\} 。 - 当我们选择加运算时,我们考虑进位情况即可,所以我们不难得到
dp_{\lfloor \frac{c + x_i + 1}{2} \rfloor, i + 1} = \min \{dp_{j, i} + 2^i \times ((x_i + c + 1) \bmod 2\} 。
- 当我们选择或运算时,当前位置一定是
所以我们就按照这样动态规划就行了。
注意到
代码
::::success[代码]
#include <bits/stdc++.h>
#ifdef __linux__
#include <bits/extc++.h>
#define getchar getchar_unlocked
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#endif
#define int long long
#define lc (p << 1)
#define rc (p << 1 | 1)
#define pii pair<int, int>
#define pdd pair<double, double>
#define pss pair<string, string>
#define ull unsigned int
#define push_back emplace_back
#define push emplace
#define min basic_operation :: my_min
#define max basic_operation :: my_max
#define abs basic_operation :: my_abs
#define chkmin basic_operation :: my_chkmin
#define chkmax basic_operation :: my_chkmax
#define lowbit basic_operation :: my_lowbit
#define sqrt sqrtl
#define double long double
#define all(x) (x).begin(), (x).end()
#define exall(x, n) (x) + 1, (x) + 1 + (n)
#define siz(x) ((int)(x).size())
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define rp(i, a, b) for (register int i = (a); i < (b); ++i)
#define pr(i, a, b) for (register int i = (a); i > (b); --i)
#define rtep(i, a, b, c) for (register int i = (a); i <= (b); i += (c))
#define pter(i, a, b, c) for (register int i = (a); i >= (b); i -= (c))
#define inl_force __attribute__((always_inline)) inline
#define fir first
#define sec second
using namespace std;
namespace basic_operation {
template<typename Tp> inl_force Tp my_min(const Tp& x, const Tp& y) { return (x < y ? x : y); }
template<typename Tp> inl_force Tp my_max(const Tp& x, const Tp& y) { return (x > y ? x : y); }
template<typename Tp> inl_force Tp my_abs(const Tp& x) { return (x >= 0 ? x : -x); }
template<typename Tp> inl_force Tp my_lowbit(const Tp& x) { return x & (-x); }
template<typename Tp> inl_force void my_chkmin(Tp& x, const Tp& y) { x = min(x, y); }
template<typename Tp> inl_force void my_chkmax(Tp& x, const Tp& y) { x = max(x, y); }
}
namespace FastIO {
template<typename Tp> inl_force Tp input() {
register Tp x = 0;
register int f = 1;
register char ch = getchar();
while (!isdigit(ch)) {
if (ch == EOF) return 0;
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
template<typename Tp> inline void output(Tp x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) output(x / 10);
putchar((x % 10) ^ 48);
}
template<typename Tp> inl_force void outputll(Tp x) { output(x); putchar(' '); }
template<typename Tp> inl_force void outputln(Tp x) { output(x); putchar('\n'); }
inl_force double double_input() { double x; scanf("%Lf", &x); return x;}
inl_force void double_output(const double& x) { printf("%Lf", x); }
inl_force char char_input() { return getchar(); }
inl_force void char_output(const char& x) { putchar(x); }
inl_force string string_input() { static char buf[1000005]; scanf("%s", buf); return string(buf); }
inl_force void string_output(const string& s) { fputs(s.c_str(), stdout); }
#define input() (input<int>())
} using namespace FastIO;
constexpr int N = 1e2 + 5;
int T, x, y, dp[2][2];
inl_force void solve() {
x = input(), y = input();
dp[0][0] = 0; dp[0][1] = dp[1][0] = dp[1][1] = 1e12;
rep(i, 0, 30) {
dp[0][(i & 1) ^ 1] = dp[1][(i & 1) ^ 1] = 1e12;
register int cur = (x >> i) & 1, now = (y >> i) & 1;
rp(j, 0, 2) {
if (dp[j][i & 1] == 1e12) continue;
if (!cur) dp[(now + j) >> 1][(i & 1) ^ 1] = min(dp[(now + j) >> 1][(i & 1) ^ 1], dp[j][i & 1] + (1 << i) * ((now + j) & 1));
else dp[(now + j) >> 1][(i & 1) ^ 1] = min(dp[(now + j) >> 1][(i & 1) ^ 1], dp[j][i & 1] + (1 << i)), dp[(now + j + 1) >> 1][(i & 1) ^ 1] = min(dp[(now + j + 1) >> 1][(i & 1) ^ 1], dp[j][i & 1] + (1 << i) * ((now + j + 1) & 1));
}
}
outputln(min(dp[0][1], dp[1][1]));
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("problem.in", "r", stdin);
freopen("problem.out", "w", stdout);
#endif
register clock_t time = clock();
T = input();
while (T--) solve();
#ifndef ONLINE_JUDGE
cerr << "\ntime: " << clock() - time << "ms\n";
#endif
return 0;
}
::::