Period
Time limit1sMemory limit1024 MB
Given a repeating decimal 0.(digits) with the repeating block written three times, print its value as a fully reduced fraction, numerator then denominator.
- Level
Easy3 of 10
- Topics
- String, Math, Number theory
- Solved
- No attempts yet
Problem
Ivica likes decimal numbers. A decimal number has an integer part, a decimal point, and a fractional part. He especially likes numbers whose integer part is zero and whose fractional part is infinite, with some digits repeating periodically. For example, in 0.333... the digit 3 repeats forever, in 0.252525... the digits 25 repeat, and in 0.57424242... the digits 42 repeat.
A student from Cambridge wrote an article explaining how such decimals can be written as fractions. Let D be the decimal number, x the number of digits that do not repeat, and y the positive number of digits in the repeating part. For D = 0.064333..., x = 3 and y = 1. For D = 0.333..., x = 0 and y = 1. There are two cases.
Case 1: x = 0.
- Multiply D by .
- Subtract D from both sides.
- The numerator is the number on the right side, and the denominator is the number next to D.
Case 2: x > 0.
- Multiply D by .
- Multiply the original D by .
- Subtract the left sides of the two results from each other, and subtract their right sides from each other.
- The numerator is the number on the right side, and the denominator is the number next to D.
In both cases, the fraction must be reduced fully. A fraction is reduced fully when no natural number divides both the numerator and the denominator. If a number has more than one pair (x, y) that fits, choose the pair with the smallest y.
Input
The first line contains a real number D (0 < D < 1), the decimal number from the statement. The group of repeating digits appears exactly three times. It also holds that x + 3y <= 15.
Output
Print the numerator on the first line and the denominator on the second line of the fraction, reduced fully.