Selling Numbers
Time limit2sMemory limit256 MB
Count how many D-digit strings, with leading zeros allowed, have exactly the memorability score S defined by palindromic and repeated substrings.
- Level
Hard8 of 10
- Topics
- Backtracking, Combinatorics, String
- Solved
- No attempts yet
Problem
Building a new telephone network costs a great deal of money. That is why the young entrepreneur Ace E. Emme plans to sell some of his trademarked Global Unique phone numbers first, then put the cash into the technical problems.
A global phone number needs many digits, so a number that is easy to recite from memory is worth more. Every number therefore gets a memorisability score, computed by the following rules.
-
Set the score to 0.
-
For each substring of length , add to the score if and the substring is a palindrome. A palindrome reads the same backwards as forwards.
-
For each pair of non-overlapping substrings and of the same length (), where appears after , add to the score once for every one of the following conditions that holds.
-
.
-
and starts at the position right after ends.
-
is equal to reversed.
-
Mr Emme prices his numbers by score, so counting how many numbers carry a given score is what lets him label them Gold Class, Diamond Class and Diamond Class Plus Plus.
Each rule applies to one substring, or to one pair of substrings, independently of every other application. A palindrome of length 5 always contains a palindrome of length 3 and always produces one pair matching the third condition of rule 3, so such a five character substring is worth at least . That is intended. For the same total length, one long pattern looks more attractive to a customer than several short ones, so it earns more.
Input
The input contains at most 11,000 test cases. Each test case is one line holding two integers () and () separated by a single space. It asks how many digit phone numbers have a memorisability score of exactly . Phone numbers with leading zeros count as valid numbers.
The last line of the input holds two zeros separated by a space, and that line is not a test case.
Output
For each test case print one line in this format.
Among D digit phone numbers, there are N with score S.
Here and are the values read from the input, and is the number of phone numbers that match.