DEADFR - Dead Fraction

Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by "...". For instance, instead of "1/3" he might have written down "0.3333...". Unfortunately, his results require exact fractions! He doesn't have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions.

To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).


There are several test cases. For each test case there is one line of input of the form "0.dddd..." where dddd is a string of 1 to 18 digits, not all zero. A line containing 0 follows the last case.


For each case, output the original fraction.




hide comments
Amitayush Thakur: 2018-12-01 19:12:02

@lenosey: Mathematically speaking 0.4746124 == 0.4746123999999..... . Infact 1 == 0.9999.... there is a wiki page on that. Read about it here:

lenosey: 2017-08-14 04:31:54

i can't understand the answer of 0.474612399... is 1186531/2500000? 1186531/2500000==0.4746124?

Last edit: 2017-08-14 04:32:32
Shashank Tiwari: 2015-08-29 15:14:03

Let me make problem statement clear ...

Input will ALWAYS be of following format :


This means a-f can be of 18 digits long followed by 3 '.' . In the beginning it will always be "0."

Now , when u r given , say, 0.abcdef... ,it doesnot specify that only last digit that is 'f' will be recurring.
It means any one of the following case can happen :

a) 0.abcdeffffffffffffffff... (f is recurring)
b)0.abcd efefefefefef... (ef is recurring)
c) defdefdef.... (def is recurring)
d)0.ab cdefcdefcdef ... (cdef is recurring)
e) 0.a bcdefbcdef...(bcdef is recurring)
f)0. abcdefabcedf...(abcdef is recurring)

Now your task is to consider all possible cases , convert then to equivalent fractions and print only that fraction which has least denominator .

Now what if 2 fractions have same minimum denominator out of these cases ? well , in that case print any .

Last edit: 2015-08-29 15:14:29
kartikay singh: 2014-12-26 09:30:47

need test cases ...
my judge shows wrong answer although i think its correct
Finally AC.. :) After several WAs..

Last edit: 2015-06-05 07:15:14
mjh: 2014-12-20 09:54:20

for the ans 1, you should print 1/1.
this cost me two wa.

Hasil Sharma: 2014-10-16 17:49:37

AC :) .. Try not to assume anything from your side ..

Gannu_raj: 2014-10-07 15:19:58

What should be output for this test case

Bharath Reddy: 2014-04-30 10:40:52

@those getting WA. check whether your code overflows for this case:

nitish rao: 2013-06-30 17:51:31

after a lot of frustration... got an AC...:) try writing the code without using any double type in order to avoid unnecessary rounding offs..(in my algo..).i've done that way..

Lalit: 2013-05-23 10:40:50

I think my code is correct and it giving correct output for all the nos that i have checked. still m getting WA..:( my id is 9326947 . plz check it and tell whats wrong with it

Added by:Camilo Andrés Varela León
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Waterloo Local Contest Sep. 27, 2003