Figure Out the Base
Time limit1sMemory limit128 MB
Given an equation with + and * over digit strings, find every base B >= 2 in which both sides evaluate to the same value.
- Level
Medium7 of 10
- Topics
- Math, Brute force, Implementation, Number theory
- Solved
- No attempts yet
Problem
What is times ? The answer is . If you thought it was , take a short break before continuing — because is the result when the arithmetic is done in base .
For an integer , every digit of a base- number is an integer between and . To read a base- number, multiply the rightmost digit by , the next digit to the left by , the next by , and so on, then add everything up.
Whether an equation is true can depend on the base being used. For example, is always true when : base cannot use the digit , so must be at least . On the other hand, an equation such as can never be true in any base.
Given an equation, write a program that determines in which bases the equation is true.
Input
Each line of input is one test case of the form EXPR=EXPR. EXPR is an expression whose length does not exceed .
Every expression is always valid and consists only of +, *, and the digits 0 through 9. No expression starts with +, and no number has unnecessary leading zeros.
The last line of input contains a single =.
Output
For each test case, print the bases in which the given equation is true.
If infinitely many bases make the equation true, print B+, where is the smallest such base.
If the number of such bases is finite, print them in ascending order separated by spaces.
If no base makes the equation true, print *.