103013: [Atcoder]ABC301 D - Bitmask

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:15 Solved:0

Description

Score : $400$ points

Problem Statement

You are given an integer $N$ and a string $S$ consisting of 0, 1, and ?. Let $T$ be the set of values that can be obtained by replacing each ? in $S$ with 0 or 1 and interpreting the result as a binary integer. For instance, if $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 $T$ less than or equal to $N$. If $T$ does not contain a value less than or equal to $N$, print -1 instead.

Constraints

  • $S$ is a string consisting of 0, 1, and ?.
  • The length of $S$ is between $1$ and $60$, inclusive.
  • $1\leq N \leq 10^{18}$
  • $N$ is an integer.

Input

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

$S$
$N$

Output

Print the answer.


Sample Input 1

?0?
2

Sample Output 1

1

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


Sample Input 2

101
4

Sample Output 2

-1

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


Sample Input 3

?0?
1000000000000000000

Sample Output 3

5

Input

题意翻译

# 题意简述 给定整数 $N(1\le N\le 10^{18})$ 和只包含字符 `0`、`1`、`?` 的字符串 $S(1\le |S|\le 60)$。 将 $S$ 视为一个二进制数,令 $T$ 为将 $S$ 中的 `?` 替换为 `0` 或 `1` 后所能得到的数字集合。请求出 $T$ 中小于等于 $N$ 的最大数字,并以十进制方式输出。 如果 $T$ 中不包含小于等于 $N$ 的数字,输出 `-1`。 Translate by @[tianbiandeshenghuo11](/user/752485)

Output

分数:$400$分

问题描述

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

输出$T$中不大于$N$的最大值。如果$T$中不存在不大于$N$的值,则输出-1

约束

  • $S$是一个由01?组成的字符串。
  • $S$的长度在$1$到$60$之间,包括$1$和$60$。
  • $1\leq N \leq 10^{18}$
  • $N$是一个整数。

输入

输入从标准输入中给出,格式如下:

$S$
$N$

输出

输出答案。


样例输入1

?0?
2

样例输出1

1

如问题描述中所示,$T=\lbrace 0,1,4,5\rbrace$。其中,$0$和$1$不大于$N$,所以你应该输出它们中最大的值,即$1$。


样例输入2

101
4

样例输出2

-1

我们有$T=\lbrace 5\rbrace$,其中不包含不大于$N$的值。


样例输入3

?0?
1000000000000000000

样例输出3

5

HINT

?可填0或者1,得到的不超过n的最大二进制数的值是多少?

加入题单

算法标签: