Palindromes

Time limit1sMemory limit128 MB

Summary
For each number in a small interval written in base b, apply reverse-and-add up to l times and count how many do not reach a palindrome.
Level

Medium7 of 10

Topics
Simulation, Math, String, Implementation
Solved
No attempts yet

Problem

Willi took part in a programming contest. One of the tasks was to check whether given strings were palindromes — strings that read the same forwards and backwards. Solving it earned him the point he needed for victory, so he decided to spend some of his free time exploring palindromes further. He realized that numbers can be palindromes too: for example, 23757322375732 is a palindrome.

After a while, Willi noticed the following: "If a number is not a palindrome, I can turn it into one by adding the number with its digits reversed." For instance, 134134 is not a palindrome, but adding its reverse 431431 gives 565565, which is a palindrome.

Playing with this on paper, he found numbers for which one step is not enough, such as 370370. Adding its reverse 073=73073 = 73 yields 443443, which is not a palindrome. His fix: if the result is not a palindrome, simply repeat the reverse-and-add step until a palindrome appears. In this example, 443+344=787443 + 344 = 787, a palindrome.

But does this always work?

Because Willi got bored with the problem, he asks for your help. Write a program that, for every number in an interval [a,b][a, b], checks whether repeatedly reversing the number and adding it to itself produces a palindrome. To make sure your program terminates, he also gives you the maximum number of iterations to apply.

Since Willi is curious — and does not have to do the work himself — he wants you to check this for numbers written in an arbitrary base, not just decimal.

Input

The first line of input contains the number of test cases TT. Each test case starts with a line containing the base bb and the maximum number of iterations ll, separated by a space. The next line contains two strings ss and ee, separated by a space, written in base bb; they denote the start and end of the interval to test.

The values satisfy the following limits:

  • The base bb of the numbers: 2≤b≤302 \le b \le 30.
  • The number of iterations ll: 1≤l≤1001 \le l \le 100.
  • ss and ee each have fewer than 100100 digits, and the interval [s,e][s, e] contains only a small number of values (at most a few hundred).

Digit values greater than 99 are written with lowercase letters (a=10,b=11,…a = 10, b = 11, \dots).

Output

The output for each test case begins with a line Scenario i:, where ii is the number of the scenario, starting at 11.

For each scenario, every number in the interval [s,e][s, e] is tested to see whether repeatedly adding its digit-reversal produces a palindrome within ll iterations. (A number that is already a palindrome is considered to terminate in 00 iterations.)

The next line contains the count of numbers for which no termination of the algorithm within ll iterations could be shown.

Examples2

  1. Example 1

    Input
    2
    10 5
    1 100
    16 10
    be00 beef
    
    Expected output
    Scenario 1:
    4
    Scenario 2:
    29
    
  2. Example 2

    Input
    1
    10 1
    1 10
    
    Expected output
    Scenario 1:
    0