题解:P11485 「Cfz Round 5」Non-breath Oblige

· · 题解

1. 题意

可进行若干次操作。每次操作,选择一个非负整数 x 并把 s 的值修改为 x,但有如下要求:

需求出令 s=t 所需代价之和的最小值。

2. 思路

a. s \ne t

如果令 s \oplus xx \oplus t 的代价最小,那么 x 在二进制中 1 的个数需要尽量的多,显而易见 x2^n−1。所以可得出代价公式:

直接运用即可。

b. s = t

不难发现结果为 0

3. 代码

#include<bits/stdc++.h>
//#pragma GCC optimize("O3","Ofast","inline","unroll-loops")
#define ll long long
using namespace std;
ll T,n,s,t; 
int main()
{
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin >> T;
    while(T--)
    {
        cin >> n >> s >> t;
        if(s==t) cout << 0;
        else
        {
            ll x=(1<<n)-1;
            cout << (s^x)+(x^t);
        }
        cout <<"\n";
    }

    return 0;
}