#include <iostream>
using namespace std;
int getHighestTwoPowerFactor(int from);
int getNearestTwoPower(int from);
int getTrains(int from, int to) {
cout << from << " ";
int count = 1;
if(from == 0) {
from = getNearestTwoPower(to);
count++;
cout << from << " ";
} else if(from % 2 != 0) {
from++;
count++;
cout << from << " ";
}
if(from == to)
return count;
int getHighestTwoPowerFactor = 1;
while(true) {
getHighestTwoPowerFactor = ::getHighestTwoPowerFactor(from);
from += getHighestTwoPowerFactor;
count++;
if(from > to) {
from -= getHighestTwoPowerFactor;
count--;
break;
} else if(from == to) {
cout << from << " ";
return count;
}
cout << from << " ";
}
while(from < to) {
while(from + getHighestTwoPowerFactor > to) {
getHighestTwoPowerFactor /= 2;
}
from += getHighestTwoPowerFactor;
count++;
cout << from << " ";
}
cout << endl;
return count;
}
int getHighestTwoPowerFactor(int from) {
if((from & (from - 1)) == 0)
return from;
int count = 0;
while(from % 2 == 0) {
from >>= 1;
count++;
}
return 1 << count;
}
int getNearestTwoPower(int from) {
if((from & (from - 1)) == 0)
return from;
int count = 0;
while(from != 0) {
from >>= 1;
count++;
}
return 1 << (count - 1);
}
int main() {
int from = 3, to = 17;
int result = getTrains(from, to);
cout << "Total steps: " << result << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGdldEhpZ2hlc3RUd29Qb3dlckZhY3RvcihpbnQgZnJvbSk7CmludCBnZXROZWFyZXN0VHdvUG93ZXIoaW50IGZyb20pOwoKaW50IGdldFRyYWlucyhpbnQgZnJvbSwgaW50IHRvKSB7CiAgICBjb3V0IDw8IGZyb20gPDwgIiAiOwogICAgaW50IGNvdW50ID0gMTsKICAgIAogICAgaWYoZnJvbSA9PSAwKSB7CiAgICAgICAgZnJvbSA9IGdldE5lYXJlc3RUd29Qb3dlcih0byk7CiAgICAgICAgY291bnQrKzsKICAgICAgICBjb3V0IDw8IGZyb20gPDwgIiAiOwogICAgfSBlbHNlIGlmKGZyb20gJSAyICE9IDApIHsKICAgICAgICBmcm9tKys7CiAgICAgICAgY291bnQrKzsKICAgICAgICBjb3V0IDw8IGZyb20gPDwgIiAiOwogICAgfQogICAgCiAgICBpZihmcm9tID09IHRvKQogICAgICAgIHJldHVybiBjb3VudDsKICAgIAogICAgaW50IGdldEhpZ2hlc3RUd29Qb3dlckZhY3RvciA9IDE7CiAgICAKICAgIHdoaWxlKHRydWUpIHsKICAgICAgICBnZXRIaWdoZXN0VHdvUG93ZXJGYWN0b3IgPSA6OmdldEhpZ2hlc3RUd29Qb3dlckZhY3Rvcihmcm9tKTsKICAgICAgICBmcm9tICs9IGdldEhpZ2hlc3RUd29Qb3dlckZhY3RvcjsKICAgICAgICBjb3VudCsrOwogICAgICAgIGlmKGZyb20gPiB0bykgewogICAgICAgICAgICBmcm9tIC09IGdldEhpZ2hlc3RUd29Qb3dlckZhY3RvcjsKICAgICAgICAgICAgY291bnQtLTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfSBlbHNlIGlmKGZyb20gPT0gdG8pIHsKICAgICAgICAgICAgY291dCA8PCBmcm9tIDw8ICIgIjsKICAgICAgICAgICAgcmV0dXJuIGNvdW50OwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IGZyb20gPDwgIiAiOwogICAgfQogICAgCiAgICB3aGlsZShmcm9tIDwgdG8pIHsKICAgICAgICB3aGlsZShmcm9tICsgZ2V0SGlnaGVzdFR3b1Bvd2VyRmFjdG9yID4gdG8pIHsKICAgICAgICAgICAgZ2V0SGlnaGVzdFR3b1Bvd2VyRmFjdG9yIC89IDI7CiAgICAgICAgfQogICAgICAgIGZyb20gKz0gZ2V0SGlnaGVzdFR3b1Bvd2VyRmFjdG9yOwogICAgICAgIGNvdW50Kys7CiAgICAgICAgY291dCA8PCBmcm9tIDw8ICIgIjsKICAgIH0KICAgIGNvdXQgPDwgZW5kbDsKICAgIHJldHVybiBjb3VudDsKfQoKaW50IGdldEhpZ2hlc3RUd29Qb3dlckZhY3RvcihpbnQgZnJvbSkgewogICAgaWYoKGZyb20gJiAoZnJvbSAtIDEpKSA9PSAwKQogICAgICAgIHJldHVybiBmcm9tOwogICAgCiAgICBpbnQgY291bnQgPSAwOwogICAgd2hpbGUoZnJvbSAlIDIgPT0gMCkgewogICAgICAgIGZyb20gPj49IDE7CiAgICAgICAgY291bnQrKzsKICAgIH0KICAgIHJldHVybiAxIDw8IGNvdW50Owp9CgppbnQgZ2V0TmVhcmVzdFR3b1Bvd2VyKGludCBmcm9tKSB7CiAgICBpZigoZnJvbSAmIChmcm9tIC0gMSkpID09IDApCiAgICAgICAgcmV0dXJuIGZyb207CiAgICAKICAgIGludCBjb3VudCA9IDA7CiAgICB3aGlsZShmcm9tICE9IDApIHsKICAgICAgICBmcm9tID4+PSAxOwogICAgICAgIGNvdW50Kys7CiAgICB9CiAgICByZXR1cm4gMSA8PCAoY291bnQgLSAxKTsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgZnJvbSA9IDMsIHRvID0gMTc7CiAgICBpbnQgcmVzdWx0ID0gZ2V0VHJhaW5zKGZyb20sIHRvKTsKICAgIGNvdXQgPDwgIlRvdGFsIHN0ZXBzOiAiIDw8IHJlc3VsdCA8PCBlbmRsOwogICAgcmV0dXJuIDA7Cn0=