#P12607. 三叉求和
三叉求和
Description
You are given an infinite ternary tree rooted at vertex . The children of vertex are the vertices , , and .
Let denote the value written on vertex . It is guaranteed that for all and , we have .
Your task is to count the number of simple paths with length equal to starting at vertex such that the sum of the values along the path equals .
However, is not completely specified: some digits in its ternary representation are replaced with the character , indicating unknown digits.
Your task is to compute the total number of such paths across all valid values of , considering all ways to replace each with a digit from .
Input Format
The first line contains a single integer .
Next line contains the ternary representation of . The ternary string may contain leading zeros and question marks, and its length is exactly .
Output Format
Print a single integer — the total number of valid simple paths modulo .
3
?12
3
3
???
19
4
0211
1
10
21??1?2??0
161
30
???1????0????1???0????2???????
744432249
Hint
Sample Explanation #1
The legal paths are:
These paths correspond to the following sequences of vertex values:
Data Range
| TestCase # | Special Constraints | |
|---|---|---|
| - | ||
| A | ||
| B | ||
| - | ||
| A | ||
| B | ||
| - | ||
| A | ||
| B | ||
| - |
A: It is guaranteed that the number of s in is no more than .
B: only contains .
It is guaranteed that for all testcases, .
京公网安备 11011102002149号