1618F - Reverse - CodeForces Solution


bitmasks constructive algorithms dfs and similar implementation math strings *2000

Please click on ads to support us..

Python Code:

a, b = map(int, input().split())
s = bin(b)[2:]
t = bin(a)[2:]
if s == t:
	print("YES")
	exit(0)
for q in [t + '1', t.strip('0')]:
	for l in range(len(s) - len(q) + 1):
		r = len(s) - len(q) - l
		if '1' * l + q + '1' * r == s or '1' * l + q[::-1] + '1' * r == s:
			print("YES")
			exit(0)
print("NO")

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define fr                first
#define sc                second
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll
#define debug(x)  cout<<(x)<<'\n';


const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=0;

int y;
string s1;

map<pair<string,string>,bool> m;
bool test(string& s,int c)
{

if(s==s1)
{
   
    return true;
}
bool ans=false;
if(m[{s,s1}])
{
    return false;
}


m[{s,s1}]=true;


{
int cnt=0;
for(int i=s.size()-1;i>=0;i--)
{
    if(s[i]=='1')
    {
        break;
    }
    s.ppb();
    cnt++;
}

reverse(all(s));

ans=ans|test(s,c+1);
reverse(all(s));

for(int i=0;i<cnt;i++)
{
s.pb('0');
}

}
if(s.length()<70)
{

s.pb('1');
reverse(all(s));
ans=ans|test(s,c+1);
reverse(all(s));
s.ppb();

}

return ans;

}



void solve(){
   
int x;
cin>>x>>y;

s1.clear();
map<int,int> m;
while(y)
{
if(y%2)
{
    s1.pb('1');
}
else s1.pb('0');

y=y/2;
}
reverse(all(s1));
string curr;
while(x)
{
if(x%2)
{
    curr.pb('1');
}
else curr.pb('0');
x=x/2;

}
reverse(all(curr));
if(test(curr,0))
{
    cout<<"YES"<<'\n';

}
else cout<<"NO"<<'\n';
    
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int t=1;
    
    while(t--) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos