#P5362. [SDOI2019] 连续子序列

[SDOI2019] 连续子序列

Description

We define the T. M.\textbf{T. M.} sequence {Tn}\{T_n\} as a Boolean sequence of the following form:

  • T0=0T_0=0.
  • T2n=TnT_{2n}=T_n.
  • T2n+1=1TnT_{2n+1}=1-T_n.

Here are the first several terms of the T. M.\textbf{T. M.} sequence: 0110100110010110100101100110100101101001100101101001011001101001\cdots.

The T. M.\textbf{T. M.} sequence is an infinite sequence, and it has many consecutive subsequences.
For example, 00, 11, 1010010100, 1001110011, and 011001011001 are all consecutive subsequences of it, while 111111 and 10001000 are not.

Now you are given a Boolean sequence (a 01 string) SS and a non-negative integer kk. Please count how many different consecutive subsequences TT of the T. M.\textbf{T. M.} sequence satisfy:

  • SS is a prefix of TT.
  • TT is formed by appending exactly kk more terms to the right of SS.

Input Format

The first line contains an integer TT, indicating that there are TT test cases.

Then follow TT lines. Each line contains a 01 string SS (representing a Boolean sequence) and a non-negative integer kk, describing one test case.

Output Format

For each test case, output one line containing an integer, indicating the number of consecutive subsequences that satisfy the conditions. Since the value may be very large, output it modulo 109+910^9+9.

5
1001 3
11001 10
00111 10
0011 20
0 100
3
4
0
6
164

Hint

Subtask 11 (2020 points): 1T1001\le T\le 100, the length of the given Boolean sequence is at most 100100, and 0k1000\le k\le 100.

Subtask 22 (2020 points): 1T1001\le T\le 100, the length of the given Boolean sequence is at most 100100, and 0k500000\le k\le 50000.

Subtask 33 (6060 points): 1T1001\le T\le 100, the length of the given Boolean sequence is at most 100100, and 0k10180\le k\le 10^{18}.

Translated by ChatGPT 5