跳到正文

题解

题解:P13712 淘汰(Easy ver.)

Yizhi_Juruo约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
获赞
1
题目
P13712

版本

  1. v1当前

题解P13712 淘汰(Easy ver.)

本题思路

运用了按位与和按位或的幂等性原理,及对于任意整数x,a

(x∧a)∧a=x∧a(x\land a)\land a=x \land a

(x∨a)∨a=x∨a(x\lor a)\lor a=x \lor a

这一特性将无限种可能的操作序列,压缩为仅5种有效操作组合(两种操作最多各执行一次)。

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int>PII;
const int N=1e6+10;
int x,y,a,b,c,d;
void solve() {
	cin>>x>>y>>a>>b>>c>>d;
	int ans=2e18;
	if(x==y){
		ans=0;
	}
	if((x&a)==y){
		ans=min(ans,c);
	}
	if((x|b)==y){
		ans=min(ans,d);
	}
	if(((x&a)|b)==y){
		ans=min(ans,c+d);
	}
	if(((x|b)&a)==y){
		ans=min(ans,c+d);
	}
	if(ans==2e18){
		cout<<-1<<endl;
	}else{
		cout<<ans<<endl;
	}
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T=1;
	cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}
原文