This page is still under construction.

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

Self-Replicating Numbers

Time limit2sMemory limit64 MB

Summary
Find every n-digit base-b number whose square ends with the same n digits.
Level

Medium7 of 10

Topics
Number theory, Math, Implementation
Solved
No attempts yet

Problem

Misha likes playing with numbers. A few days ago he found that 93762=879093769376^2 = 87909376, and the last four digits of the square are 93769376 again. Misha calls a number like this self-replicating.

Misha knows bases other than ten, so he also wants the self-replicating numbers of base 2 and base 16. Given a base bb and a length nn, find every self-replicating number that has nn digits in base bb.

A number xx is an nn-digit self-replicating number in base bb when both of the following hold.

  • Written in base bb without leading zeros, xx has exactly nn digits. The value 00 counts as the one-digit string 0.
  • The last nn digits of x2x^2 in base bb are the base bb representation of xx, that is, x2≡x(modbn)x^2 \equiv x \pmod{b^n}.

Input

The first line contains the base bb and the length nn, separated by a single space. (2≤b≤362 \le b \le 36, 1≤n≤20001 \le n \le 2000)

Output

On the first line print KK, the number of nn-digit self-replicating numbers in base bb. On each of the next KK lines print one such number in base bb, ordered from the smallest value to the largest.

When b>10b > 10, use the uppercase letters A to Z for the digit values 1010 to 3535. If no number qualifies, print only 00 on the first line.

Examples1

  1. Example 1

    Input
    12 6
    
    Expected output
    2
    1B3854
    A08369