102005: [AtCoder]ABC200 F - Minflip Summation

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

Description

Score : $600$ points

Problem Statement

We have a string $S$ consisting of 0, 1, and ?. Let $T$ be the concatenation of $K$ copies of $S$.
By replacing each ? in $T$ with 0 or 1, we can obtain $2^{Kq}$ strings, where $q$ is the number of ?s in $S$. Solve the problem below for each of these strings and find the sum of all the answers, modulo $(10^9+7)$.

Let $T'$ be the string obtained by replacing ? in $T$. We will repeatedly do the operation below to make all the characters in $T$ the same. At least how many operations are needed for this?

  • Choose integers $l$ and $r$ such that $1 \le l \le r \le |T'|$, and invert the $l$-th through $r$-th characters of $T$: from 0 and 1 and vice versa.

Constraints

  • $1 \le |S| \le 10^5$
  • $1 \le K \le 10^9$

Input

Input is given from Standard Input in the following format:

$S$
$K$

Output

Print the answer as an integer.


Sample Input 1

101
2

Sample Output 1

2

We have $T=$ 101101, which does not contain ?, so we just need to solve the problem for the only $T'=$ 101101.
We can make all the characters the same in two operations as, for example, 101101 $\rightarrow$ 110011 $\rightarrow$ 111111.
We cannot make all the characters the same in one or fewer operations.


Sample Input 2

?0?
1

Sample Output 2

3

We have four candidates for $T'$: 000, 001, 100, and 101.


Sample Input 3

10111?10??1101??1?00?1?01??00010?0?1??
998244353

Sample Output 3

235562598

Since the answer can be enormous, find it modulo $(10^9+7)$.

Input

题意翻译

给定一个仅含 `0`,`1` 和 `?` 的字符串 $S$ 和一个参数 $K$,将 $S$ 复制 $K$ 次得到字符串 $T$。 (即 $T=SSS...S$,共 $K$ 个 $S$) 你可以对 $T$ 进行若干次操作:每次选取一组 $l$ 和 $r$,将 $[l, r]$ 内所有 `1` 变为 `0`,所有 `0` 变为 `1`。求将 $T$ 中的所有字符变为同一种所需的最小操作次数。 特别地,字符 `?` 代表此处还没有填上。`?` 处既可填 `0` 亦可填 `1`,但你需要将填 `0` 和 `1` 的方案都计入最后的贡献之中。 形式化地讲,若 $S$ 中有 $q$ 个字符为 `?`,你需要计算所有 $2^{Kq}$ 种可能的字符串各自所需要的最小操作数,并将它们的总和作为最终答案。 若仍不理解,可以参考样例2。 答案对 $10^9+7$ 取模。

加入题单

上一题 下一题 算法标签: