#P8194. [USACO22FEB] Phone Numbers P

    ID: 9268 远端评测题 4000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DPUSACO2022动态规划优化状态合并

[USACO22FEB] Phone Numbers P

Problem Description

Bessie has gotten a new phone with a 9-key keypad, laid out as follows:

123
456
789

Bessie is in a hurry and is trying to type a given phone number, so she decides to save time by using one of her hooves to press multiple buttons at once. Specifically, Bessie's hoof may press a single key, two keys that share an edge (there are 1212 such possibilities), or the four keys that form a square (12451245, 23562356, 45784578, 56895689).

For example, if the phone number Bessie wants to type is 123659874123659874, she might try to save time by pressing keys in the following way:

  1. Press 11 and 22 at the same time.
  2. Press 33.
  3. Press 6,5,9,86,5,9,8 at the same time.
  4. Press 77 and 44 at the same time.

Unfortunately, Bessie greatly overestimated her ability to do this task—if Bessie's hoof presses multiple keys at the same time, then all of those keys will be entered in an arbitrary order. So if Bessie tries to press keys in the sequence above, the phone number she ends up typing could be 123596847123596847 or 213659874213659874 (or any other possible sequence).

Given a sequence that Bessie has already typed, compute the number of phone numbers she might have intended to type, modulo 109+710^9+7.

Input Format

The first line contains an integer TT (1T101\le T\le 10), representing the number of independent test cases.

The next TT lines each contain a non-empty digit string consisting only of digits from 11 to 99. It is guaranteed that the total length of all digit strings in the input does not exceed 10510^5.

Output Format

For each test case, output the number of phone numbers Bessie might have intended to type, modulo 109+710^9+7.

5
1478
4455
5968
31313211
123659874
5
2
24
3
255

Hint

Sample Explanation

For the first test case, Bessie might have intended to type one of the following five phone numbers:

1478
1487
4178
4187
1748

For example, if Bessie intended to type 41874187, she could have tried to press 11 and 44 at the same time, and then press 77 and 88 at the same time.

For the third test case, since these digits form a square, any permutation of the typed sequence could be the phone number Bessie intended.

Constraints

  • For test cases 232\sim 3, the length of every phone number is at most 88.
  • For test cases 454\sim 5, each phone number contains only 1,21,2 and 33.
  • For test cases 676\sim 7, each phone number does not contain 55.
  • For test cases 898\sim 9, each phone number contains only 5,6,8,95,6,8,9.
  • For test cases 101210\sim 12, the total length of all phone numbers does not exceed 10210^2.
  • For test cases 131513\sim 15, the total length of all phone numbers does not exceed 10310^3.
  • For test cases 161816\sim 18, the total length of all phone numbers does not exceed 10410^4.
  • For test cases 192119\sim 21, there are no additional constraints.

Translated by ChatGPT 5