#abc301d. D - Bitmask

D - Bitmask

Score : 400400 points

问题描述

给定一个整数 NN 和一个由 01? 组成的字符串 SS。设 TT 是通过将 SS 中的每个 ? 替换为 01,并将结果解释为二进制整数所能得到的所有值的集合。例如,如果 S=S= ?0?,我们有 $T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace$。

输出(以十进制整数形式)TT 中小于等于 NN 的最大值。如果 TT 不包含小于等于 NN 的值,则输出 -1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given an integer NN and a string SS consisting of 0, 1, and ?. Let TT be the set of values that can be obtained by replacing each ? in SS with 0 or 1 and interpreting the result as a binary integer. For instance, if S=S= ?0?, we have $T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace$.

Print (as a decimal integer) the greatest value in TT less than or equal to NN. If TT does not contain a value less than or equal to NN, print -1 instead.

Constraints

  • SS is a string consisting of 0, 1, and ?.
  • The length of SS is between 11 and 6060, inclusive.
  • 1N10181\leq N \leq 10^{18}
  • NN is an integer.

Input

The input is given from Standard Input in the following format:

SS

NN

Output

Print the answer.

Sample Input 1

?0?
2

Sample Output 1

1

As shown in the problem statement, T={0,1,4,5}T=\lbrace 0,1,4,5\rbrace. Among them, 00 and 11 are less than or equal to NN, so you should print the greatest of them, 11.

Sample Input 2

101
4

Sample Output 2

-1

We have T={5}T=\lbrace 5\rbrace, which does not contain a value less than or equal to NN.

Sample Input 3

?0?
1000000000000000000

Sample Output 3

5

update @ 2024/3/10 08:27:14