Math Homework
Time limit1sMemory limit128 MB
Count N-digit strings, leading zeros allowed, whose divisibility by each of 1 to 6 matches a given pattern, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Matrix, Number theory, Dynamic programming
- Solved
- No attempts yet
Problem
Dongil got a hard math assignment and it is due tomorrow, so barely any time is left.
The assignment is division practice. The goal is to learn the process of dividing an digit number by a small number. An digit number here may start with 0, and a number written with leading zeros still counts as an digit number.
Every question on the assignment has the same shape.
How many two digit integers are divisible by 6 and not divisible by 5?
The answers to that question are 06, 12, 18, 24, 36, 42, 48, 54, 66, 72, 78, 84 and 96, so there are 13 of them.
Note that 0 is divisible by every positive integer.
Too many numbers satisfy the conditions for Dongil to list them one by one, so he decided to find only how many there are.
Input
The first line contains the number of test cases . ()
Each of the next lines contains an integer and a string of length 6, separated by a space.
means the numbers to count have digits. ()
The string consists only of the characters 0, 1 and 2. If its -th character is 0, the number must not be divisible by . If it is 1, the number must be divisible by . If it is 2, divisibility by does not matter. ()
Output
For each test case, print on one line the number of digit numbers that satisfy the conditions, modulo 1,000,000,007.