Trivial bases
Time limit3sMemory limit2048 MB
Given A, B, and C as base-10 digit lists, find a base b below 10^19 where A times B equals C, or report impossible.
- Level
Medium7 of 10
- Topics
- Math, Binary search, Implementation
- Solved
- No attempts yet
Problem
Mr. Trump recently won the US presidential election. Right after the vote, he called Mr. Malnar to check whether children across America can still take part in COCI, the international version of the Croatian Open Competition in Informatics.
The formal part of that call has not been made public. But the informal part leaked:
Mr. Malnar: So, what is new across the pond that nobody writes about in the news?
Mr. Trump: Nothing special. We recently made contact with aliens, but they are not too smart. If it comes to war, I am sure we will win.
Mr. Malnar: How do you know they are not too smart?
Mr. Trump: We intercepted the math homework of an alien elementary schooler. The kid wrote that . Fine, the kid doesn't know better, but the teacher graded it as correct. I'm telling you, they are not smart. . .
Mr. Malnar: Don't those aliens have four tentacles in total on their hands?
Mr. Trump: Wow, man, how do you know that?
Mr. Malnar: Oh, Donald, you are the one who isn't smart. . .
Of course, Mr. Malnar immediately noticed that is valid in base .
Your task is similar. For a given expression of the form , find a base in which the expression is correct and which is strictly less than .
Input
The input consists of three lines. The first line gives the number , the second line gives , and the third line gives .
Each line consists of:
- The number of digits ().
- numbers (, ), the values of the digits of the number written in base . The most significant digit is , and the least significant digit is .
Output
Print a single line with a base () in which the expression holds. If no such base exists, print impossible. If several bases work, print any of them.
Hint
In base 13, the second sample works out as follows: