My issue
My code
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t,a,b;
cin>>t;
while(t--){
cin>>a>>b;
}
return 0;
}
Problem Link: CodeChef: Practical coding for everyone
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t,a,b;
cin>>t;
while(t--){
cin>>a>>b;
}
return 0;
}
Problem Link: CodeChef: Practical coding for everyone
@deepakjdh31
The logic is first make a as power of 2 by dividing it by 2 when it become power of two then make a as b either by dividing by 2 or multiplying it by 2 depending upon b.
In this way u can count the minimum number of steps
You just neet to verify if a
is a power of 2. In that case, you need to now how far this power of 2 is from b, which is already a power of 2. (ex: 2 and 8, 2=2^1 and 8=2^3, so in this case 2 is far from 8 by 2 powers of 2).
But, if a
is not a power of two, yo need to convert it to the nearest power of 2 and count the number of steps. ex: if a=5
, by doing the first operation (only one operation) a=(a-1)/2
then a=2
(the only way to do this is by dividing, never multiply by 2). So when a
gets a power of 2, then do what I explained above.
This will be helpful: lsb(a) = (a & -a)
. Here, lsb() is the least significant byte (you can search about it later). So if you have a power of two, the lsb() of this number is the same number. In other words, to check if a
is a power of 2, just make if ( a == (a & -a) )
, and this will make all the work.
Good luck!!!