跳到正文

题解

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

fish_love_cat约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月1日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟
获赞
1
题目
P12682

版本

  1. v1当前

奶龙题啊。


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

显然可以 KMP。


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

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

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

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

不如转成字符串去做。

C++
#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;
}
原文
存档评论1 条抓取于