题解:P17594 「CZOI-R8」Nailoong and Operations

· · 题解

题目大意

给定两个非负整数 x, y,重复以下操直到 x 变成 0,令 p = \operatorname{lowbit}(x):

我们需要求所有操作后的 y 的最小值。

题目解法

我们考虑从低到高按位进行动态规划。

我们定义 dp_{j, i} 表示当前进位情况为 j,当前我们在第 i 为时的最小代价。

设 x_i 表示 x 的第 i 位,y_i 表示 y 的第 i 位。那么,我们可以从 2 个方面考虑如何进行转移。

所以我们就按照这样动态规划就行了。

注意到 i 的下一位只与第 i 位有关,所以这里可以滚动数组优化。

代码

::::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;
}

::::