#P13046. [GCJ 2021 Finals] Divisible Divisions
[GCJ 2021 Finals] Divisible Divisions
题目描述
We have a string consisting of decimal digits. A division of is created by dividing into contiguous substrings. For example, if is 0145217, two possible divisions are 014 5 21 7 and 0 14 52 17. Each digit must be used in exactly one substring, and each substring must be non-empty. If has digits, then there are exactly possible divisions of it.
Given a positive integer , a division of is called divisible by if for every pair of consecutive substrings, at least one of the integers they represent in base 10 is divisible by . If , the first example division above is divisible because 014, 21, and 7 represent integers divisible by 7. The second example division is not divisible because 52 and 17 are consecutive substrings and neither represents an integer divisible by 7. Dividing 0145217 as 0145217 is divisible by any because there are no pairs of consecutive substrings.
Given and , count how many divisions of exist that are divisible by . Since the output can be a really big number, we only ask you to output the remainder of dividing the result by the prime ().
输入格式
The first line of the input gives the number of test cases, . lines follow. Each line represents a test case with a string of digits and a positive integer , as mentioned above.
输出格式
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is the number of different divisions of that are divisible by , modulo the prime ().
3
0145217 7
100100 10
5555 12
Case #1: 16
Case #2: 30
Case #3: 1
提示
Sample Explanation
In Sample Case #1, all divisible divisions of are:
- 0145217,
- 0 145217,
- 0 14 5217,
- 0 14 5 217,
- 0 14 5 21 7,
- 0 14 521 7,
- 0 145 217,
- 0 145 21 7,
- 0 14521 7,
- 014 5217,
- 014 5 217,
- 014 5 21 7,
- 014 521 7,
- 0145 217,
- 0145 21 7, and
- 014521 7.
In Sample Case #2, there are ways to divide in total. To get two consecutive substrings to not be divisible by 10, we need both of them to not end in 0. The only 2 ways of doing that are and , which means the other 30 divisions of are divisible by 10.
In Sample Case #3, no possible substring represents an even integer, which in turn means it is not divisible by 12. Therefore, the only way to not have two consecutive substrings that are not divisible by 12 is to not have two consecutive substrings at all, which can be done in only 1 way: 5555.
Limits
- .
- .
Test Set 1 (10 Pts, Visible Verdict)
- the length of .
Test Set 2 (35 Pts, Hidden Verdict)
- the length of .