淘汰(Easy ver.)题解

· · 题解

这里提供一种乱搞做法。

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

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

#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
*/