575 - Skew Binary

Same as converting from binary to decimal but with $2^{k + 1} - 1$ as factor.

Time complexity: $O( n )$
Memory complexity: $O( n )$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    string n;
    while (cin >> n and n != "0")
    {
        int m = (int)(n).size(), x = 0;
        reverse(begin(n), end(n));
        for (int i = 0; i < m; ++i)
            x += ((1LL << (i + 1)) - 1) * (n[i] - '0');
        cout << x << endl;
    }
    return 0;
}