跳到正文

题解

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

违规用户名1764483约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年10月4日
收录
2026年10月4日
更新
2026年11月14日
篇幅
约 11 分钟
题目
P17594

版本

  1. v1当前

题目大意

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

  • 对 xx 进行这个操作:x←x−px\gets x - p。
  • 对 yy 进行以下两个操作中的一个操作:
    • y←y+py\gets y + p
    • y←yor⁡py\gets y\operatorname{or} p

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

题目解法

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

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

设 xix_i 表示 xx 的第 ii 位,yiy_i 表示 yy 的第 ii 位。那么,我们可以从 22 个方面考虑如何进行转移。

  • 当 xi=0x_i = 0 时,我们毫无疑问只能选择从低位进位而来的选择,设进位情况为 cc,那么我们可以得到 dp⌊c+xi2⌋,i+1=min⁡{dpj,i+2i×((xi+c) mod 2)}dp_{\lfloor \frac{c + x_i}{2} \rfloor, i + 1} = \min \{dp_{j, i} + 2^i \times ((x_i + c) \bmod 2)\}。
  • 当 xi=1x_i = 1 时,我们既可以选择让这一位使用或运算,也可以选择使用加运算,那么我们分情况考虑:
    • 当我们选择或运算时,当前位置一定是 11,所以我们不难得到 dp⌊c+xi2⌋,i+1=min⁡{dpj,i+2i}dp_{\lfloor \frac{c + x_i}{2} \rfloor, i + 1} = \min \{dp_{j, i} + 2^i\}。
    • 当我们选择加运算时,我们考虑进位情况即可,所以我们不难得到 dp⌊c+xi+12⌋,i+1=min⁡{dpj,i+2i×((xi+c+1) mod 2}dp_{\lfloor \frac{c + x_i + 1}{2} \rfloor, i + 1} = \min \{dp_{j, i} + 2^i \times ((x_i + c + 1) \bmod 2\}。

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

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

代码

代码
C++
#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;
}
原文