#P15962. [ICPC 2018 Jakarta R] Binary String

[ICPC 2018 Jakarta R] Binary String

题目描述

A binary string is a non-empty sequence of 0’s and 1’s, e.g., 010110, 1, 11101, etc. Ayu has a favorite binary string SS which contains no leading zeroes. She wants to convert SS into its decimal representation with her calculator.

Unfortunately, her calculator cannot work on any integer larger than KK and it will crash. Therefore, Ayu may need to remove zero or more bits from SS while maintaining the order of the remaining bits such that its decimal representation is no larger than KK. The resulting binary string also must not contain any leading zeroes.

Your task is to help Ayu to determine the minimum number of bits to be removed from SS to satisfy Ayu’s need.

For example, let S=1100101S = 1100101 and K=13K = 13. Note that 11001011100101 is 101101 in decimal representation, thus, we need to remove several bits from SS to make it no larger than KK. We can remove the 3rd3^{rd}, 5th5^{th}, and 6th6^{th} most significant bits, i.e. 1100101110111\underline{0}0\underline{10}1 \to 1101. The decimal representation of 11011101 is 1313, which is no larger than K=13K = 13. In this example, we removed 33 bits, and this is the minimum possible (If we remove only 22 bits, then we will have a binary string of length 55 bits; notice that any binary string of length 55 bits has a value of at least 1616 in decimal representation).

输入格式

Input begins with a line containing an integer KK (1K2601 \leq K \leq 2^{60}) representing the limit of Ayu’s calculator. The second line contains a binary string SS (1S601 \leq |S| \leq 60) representing Ayu’s favorite binary string. You may safely assume SS contains no leading zeroes.

输出格式

Output contains an integer in a line representing the minimum number of bits to be removed from SS.

13
1100101
3
13
1111111
4

提示

Explanation for the sample input/output #1

This sample is illustrated by the example given in the problem description above.

Explanation for the sample input/output #2

Ayu must remove 44 bits to get 111111, which is 77 in its decimal representation.