问题描述:
解题思路:
a通过位移可以减少1的个数但不会增加1的个数以及改变1之间的间隔,所以变成b只需要判断去掉头尾0后的b是不是a的子串即可。
题解:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const int mod = 1000000007;
void modify(int n,string &s) // &s 全局使用, 能够改变传进来的变量本身
{
int st = 0, ed = 31;
while(st < ed && ((n >> st) & 1) == 0)st++;
while(st < ed && ((n >> ed) & 1) == 0)ed--; // 相当于从左边的第0个开始
for(int i = ed; i >= st; i--)
s += ((n >> i) & 1) + '0'; // 这里加0是加ascii值使数字1变为字符1
}
void solve()
{
ll a,b;
string aa,bb;
cin >> a >> b;
modify(a,aa),modify(b,bb);
if(aa.find(bb) != string::npos)cout << "Yes" << '\n'; // 字符串比较函数,即bb在aa里就表示找到
else cout << "No" << '\n';
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _;
cin >> _;
while(_ --) solve();
return 0;
}
知识点:二进制位