Self-Replicating Numbers
Time limit2sMemory limit64 MB
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 , and the last four digits of the square are 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 and a length , find every self-replicating number that has digits in base .
A number is an -digit self-replicating number in base when both of the following hold.
- Written in base without leading zeros, has exactly digits. The value counts as the one-digit string
0. - The last digits of in base are the base representation of , that is, .
Input
The first line contains the base and the length , separated by a single space. (, )
Output
On the first line print , the number of -digit self-replicating numbers in base . On each of the next lines print one such number in base , ordered from the smallest value to the largest.
When , use the uppercase letters A to Z for the digit values to . If no number qualifies, print only on the first line.