题解:P12682 【MX-J15-T2】叉叉学习位运算

· · 题解

奶龙题啊。

首先观察到溢出可以将首尾部分 1 删除,那么题面就转化为给两个 \texttt{01} 串 a,b,求 b 是否为 a 的子串。

显然可以 KMP。

上面是口胡,或许直接转成字符串写 KMP 可以躲开这题关于位运算的一大堆坑,但是风铃草并没有选择这么做。

我们把 b 在不溢出的前提下靠到右侧,然后让 a 不断右移并自然溢出,然后利用位运算判断是否为子串。

但是本题数据范围有点大,ull 我不知道为什么玄学挂掉了,开 __int128 然后发现位运算失效了。

挺玄乎的,所以最后就搞的跟暴力匹配差不多了。

不如转成字符串去做。

#include<bits/stdc++.h>
#define int __int128
using namespace std;
int read(){
    int sum=0,fish=1;
    char c=getchar_unlocked();
    while((c<'0'||c>'9')&&c!='-')c=getchar_unlocked();
    if(c=='-')fish=-1,c=getchar_unlocked();
    while(c>='0'&&c<='9')sum=sum*10+(c-'0'),c=getchar_unlocked();
    return sum*fish;
}
void print(int x){
    if(x<0)putchar_unlocked('-'),x=-x;
    if(x<10)putchar_unlocked(x+'0');
    else print(x/10),putchar_unlocked(x%10+'0');
}
void solve(){
    int a=read(),b=read();
    while(!(b&1)&&b)
        b>>=1;
    // print(b);
    int c=b,d=b;
    for(int i=0;d;d>>=1,i++){
        int x=1;
        for(int j=1;j<=i;j++)x<<=1;
        c|=x;
    }
    // print(c);
    do{
        if((a&c)==b){
            puts("Yes");
            return;
        }
        a>>=1;
    }while(a);
    puts("No");
}
signed main(){
    int t=read();
    while(t--)solve();
    return 0;
}