题解P13712 淘汰(Easy ver.)
本题思路
运用了按位与和按位或的幂等性原理,及对于任意整数x,a
这一特性将无限种可能的操作序列,压缩为仅5种有效操作组合(两种操作最多各执行一次)。
AC代码
#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;
}