跳到正文

题解

淘汰(Easy ver.)题解

Cosmos_zzx约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年8月15日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 3 分钟
获赞
2
题目
P13712

版本

  1. v1当前

这里提供一种乱搞做法。

我们可以发现:对于一个 xx,x AND ax\ \text{AND}\ a 后再  AND a\ \text{AND}\ a 的值还是 x AND ax\ \text{AND}\ a,同理,连续做两次  OR\ \text{OR} 也是没有用的。

于是,对于一个 xx,要么直接 AND a\text{AND}\ a 一次,要么直接 OR b\text{OR}\ b 一次,看等不等于 yy,否则重复一千次,每次 AND\text{AND} 或 OR\text{OR} 交替进行,如果变成 yy 就输出答案即可。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
#define getchar_unlocked() getchar()
inline int read() {int x=0,ff=1;char ch=getchar_unlocked();while(ch<48||ch>57) {if(ch==45)ff=-1;ch=getchar_unlocked();}while(ch>=48&&ch<=57) x=(x<<3)+(x<<1)+(ch^48),ch=getchar_unlocked();return x*ff;}
inline void write(int x,char ch){if(!x){putchar(48),putchar(ch);return;}if(x<0)putchar(45),x=-x;char a[20];int i=0;while(x)a[++i]=x%10+48,x/=10;for(;i;i--)putchar(a[i]);putchar(ch);}
const int N=1e6+114;
int T;
int x,y,a,b,c,d;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>x>>y>>a>>b>>c>>d;
		if(x==y)cout<<0<<endl;
		else if((x&a)==y)cout<<c<<endl;
		else if((x|b)==y)cout<<d<<endl;
		else
		{
			int sum=0,ans=1e18;bool flag=0;
			int xx=x;
			for(int i=1;i<=2000;i++)
			{
				if(i%2)x&=a,sum+=c;
				else x|=b,sum+=d;
				if(x==y)
				{
					flag=1;
					break;
				}
			}
			ans=min(ans,sum);
			sum=0;x=xx;
			for(int i=1;i<=2000;i++)
			{
				if(i%2==0)x&=a,sum+=c;
				else x|=b,sum+=d;
				if(x==y)
				{
					flag=1;
					break;
				}
			}
			ans=min(ans,sum);
			if(!flag)cout<<-1<<endl;
			else cout<<ans<<endl;
		}
	}
	return 0;
}
/*
1
5
2 4 2 5 2
1 3 2 5 4
*/
原文
存档评论2 条抓取于