This page is still under construction.

Parts of this page are still being built. What you see may change.

Selling Numbers

Time limit2sMemory limit256 MB

Summary
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.

  1. Set the score to 0.

  2. For each substring of length LL, add LL to the score if L≥2L \ge 2 and the substring is a palindrome. A palindrome reads the same backwards as forwards.

  3. For each pair of non-overlapping substrings AA and BB of the same length LL (L≥2L \ge 2), where BB appears after AA, add LL to the score once for every one of the following conditions that holds.

    1. A=BA = B.

    2. A=BA = B and BB starts at the position right after AA ends.

    3. AA is equal to BB 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 5+3+25 + 3 + 2. 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 DD (0<D<120 < D < 12) and SS (0≤S<10000 \le S < 1000) separated by a single space. It asks how many DD digit phone numbers have a memorisability score of exactly SS. 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 DD and SS are the values read from the input, and NN is the number of phone numbers that match.

Examples2

  1. Example 1

    Input
    2 2
    3 7
    3 0
    0 0
    
    Expected output
    Among 2 digit phone numbers, there are 10 with score 2.
    Among 3 digit phone numbers, there are 10 with score 7.
    Among 3 digit phone numbers, there are 720 with score 0.
    
  2. Example 2

    Input
    1 0
    1 1
    2 0
    4 22
    0 0
    
    Expected output
    Among 1 digit phone numbers, there are 10 with score 0.
    Among 1 digit phone numbers, there are 0 with score 1.
    Among 2 digit phone numbers, there are 90 with score 0.
    Among 4 digit phone numbers, there are 10 with score 22.