All problems

Problem 547

Steps to Reduce a Binary Number to One

Medium · 20 pts · String, Bit Manipulation, Simulation

Problem

You are given a positive integer written in binary. Repeat the following until the number becomes 1: if it is even, divide it by 2; if it is odd, add 1 to it. Count the steps. The number can have hundreds of digits, so work on the binary string directly: scan from the last digit to the first, keeping a carry, and count one step for a halving and an extra step whenever the current bit (with carry) is odd.

Input

A single binary string S with no leading zeros.

Output

Print the number of steps needed to reach 1.

Constraints

1 <= |S| <= 500; S[0] = 1

Example input

1101

Example output

6

Explanation

1101 is 13. The steps are 13 → 14 → 7 → 8 → 4 → 2 → 1: add 1, halve, add 1, halve three times. That is 6 steps.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Coprime Pairs Next: Nth Prime