跳到正文

题解

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

munian__love_wang约 2 分钟
原文

这是 2025年12月19日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月19日
更新
2025年12月20日
篇幅
约 2 分钟
题目
P11485

版本

  1. v2当前
  2. v1

题意

给定 aa 组数据,每组数据有3个值分别为 nn,ss,tt,选择一个非负整数 xx,把 ss 的值修改为 xx。

  • 必须满足 x<2nx<2n;
  • 必须满足 s∣x=2n−1s|x=2^n−1,其中 ∣| 为按位或运算;
  • 你需要花费 s⊕xs⊕x 的代价,其中 ⊕⊕ 为按位异或运算。

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

思路

那么无疑就是两种情况

  • s==ts==t

无疑答案为0。

  • s≠ts \ne t

那么也就是 [s⊕(2n−1)]+[(2n−1)⊕t][s⊕(2 n −1)]+[(2 n −1)⊕t],也就是 (s(1<<n)−1)+((1<<n)−1t)(s^(1<<n)-1)+((1<<n)-1^t)。

那么代码也就是下面这个样子

C++
for(int i=1;i<=a;i++){
		cin>>n>>s>>t;
		if(s==t){
			cout<<0;//s==t时答案为0
		}else{
			maxn=(1<<n)-1;
			cout<<(s^maxn)+(maxn^t);//贪心
		}
		cout<<endl;//别忘了换行
	}

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
int a,n,s,t,maxn;//分别是几组数据与每组数据的测试点
int main(){
	cin>>a;//a组数据
	for(int i=1;i<=a;i++){
		cin>>n>>s>>t;
		if(s==t){
			cout<<0;//s==t时答案为0
		}else{
			maxn=(1<<n)-1;
			cout<<(s^maxn)+(maxn^t);//贪心
		}
		cout<<endl;//别忘了换行
	}
	return 0;
}

第一次写题解给过了吧qwq