Problem 104: Aliens Multiply Rapidly
(U.M. ACM Runoffs 2001, problem 4)
Description
It is generally believed that the best way to communicate with aliens
if we ever come across any will be with the language of mathematics.
Senator Cletus S. J. Yokell (Rep., Georgia), not quite understanding
the idea, has decided that we must not look bad in front of the Martians
by being slow in our calculations. To ensure this, he has appropriated
a shit-load of money from congress to fund the construction of an
alien-compatible calculator.
As we have never met a Martian, we don't know how many fingers they've got,
and therefore we don't know what base they will represent numbers in. However,
the senator is certain that Martians will not be superior to us, so we
are guaranteed the base will not be more than 10.
You must write a program that can perform arithmetical calulations in
any base (between 2 and 10 inclusive).
Input Format
The input will contain a number of test cases, each occupying two lines.
The first line of each case will be the base, B, written as a normal
decimal integer. A value of zero for B indicates the end of the input, and
should not be processed.
The second line of each case will be an expression
involving base-B numbers and the operators +, -,
*, and /. There will be no other symbols, not even spaces,
on the line. Operations should be performed in strictly left-to-right order,
there are no "priorities" attached to operators.
Output Format
For each test case, print out the value of the expression, in base-B,
alone on a single line.
Limits
You are assured that no calculation (intermediate results included) will
produce a result greater than 1,000,000,000 (decimal) or less than zero.
Nobody will ever try to divide by zero, and no division will ever
produce a result that is not an exact integer.
Sample Input
10
123+456+1
2
11111*1000+11
10
2+3*4
5
12*40
0
Sample Output
580
11111011
20
1030
End of Problem