Positional number systems
Time limit1sMemory limit1024 MB
Write integer R in a positional base B, which may be negative, fractional, or a reciprocal of an integer.
- Level
Medium4 of 10
- Topics
- Math, Implementation, Number theory, Simulation
- Solved
- No attempts yet
Problem
We are used to writing numbers in decimal. Writing "123" really means .
Binary shows up often too. The binary form of is "1111011", which means .
The base of a positional system does not have to be a natural number. Written in base , the number becomes "283", which means .
The base does not even have to be an integer. In base the number is written "22122.02012122", where the part after the point runs on forever to the right.
Bases smaller than make sense as well. In such a system the notation comes out mirrored, because place values grow as you move right from the point. In base the number is written "3.21", which means .
You are given an integer and a base . Print the representation of in base .
Input
The first line has the integer in decimal (), at most digits.
The second line has the base in decimal. Either is an integer with , or is an integer with .
Output
Let when , and when . In both cases is an integer, and the representation uses the digits to .
When , print the digits of in base on one line, from the highest place value down to the place. Print no point and no leading zero.
When , print the digit of the place first, then a point, then the digits of the , , places. The last digit printed is not . If there is only one digit, print it without a point.
If , print "0".
Exactly one representation follows these rules, and it equals with no rounding.