Multiplication in Base N (tiny)
Time limit1sMemory limit1024 MB
Multiply two integers A and B written in base N, where N may be negative, and print the product in base N.
- Level
Medium5 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
This problem differs from N-base Multiplication (HUGE) only in the length limits on and .
Write a program that multiplies two integers and written in base .
The digits of and map to ASCII characters in order. is ! (ASCII 33), is " (ASCII 34), and so on, so is the character with code . A negative sign is written as ~ (ASCII code ). It appears only at the front, at most once, and only when is positive.

ASCII character mapping table for each digit
The examples below show numbers written as described above. To make them easier to read, each digit is wrapped in square brackets.
"#&
~"#1
*!-
LUMI
may be a negative integer. See the hint for how negative bases work.
Input
The first line contains the base .
The second and third lines contain and , each written in base . Each of and has at most characters when written in base .
Neither nor has an unnecessary leading !. ~ appears only when is positive, and at most once at the front. Note that can be negative.
Output
Print the product of and in base .
Hint
In a negative base , a carry into the next position on the left multiplies the value by , just as in a positive base. The only difference is that the digit in each position is limited to .
For example, in base is in base .
.