#abc200f. F - Minflip Summation

F - Minflip Summation

Score : 600600 points

问题描述

我们有一个由 01? 组成的字符串 SS。令 TT 为将 SS 连接 KK 次得到的字符串。 通过将 TT 中的每个 ? 替换为 01,我们可以得到 2Kq2^{Kq} 个不同的字符串,其中 qqSS? 的数量。针对这些字符串中的每一个解决以下问题,并求出所有答案之和,结果对 (109+7)(10^9+7) 取模。

TT' 表示通过替换 TT 中的 ? 得到的字符串。我们将重复执行以下操作以使 TT 中的所有字符变得相同。至少需要多少次操作才能实现这一点?

  • 选择整数 llrr,满足 1lrT1 \le l \le r \le |T'|,然后反转 TT 中第 ll 到第 rr 个字符:即将 01 互换。

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

Problem Statement

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

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

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

Constraints

  • 1S1051 \le |S| \le 10^5
  • 1K1091 \le K \le 10^9

Input

Input is given from Standard Input in the following format:

SS

KK

Output

Print the answer as an integer.

Sample Input 1

101
2

Sample Output 1

2

We have T=T= 101101, which does not contain ?, so we just need to solve the problem for the only T=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 TT': 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 (109+7)(10^9+7).

update @ 2024/3/10 09:12:29