Problem:


循環小数

 2 つの正の整数 p, q を入力し、 p / q を小数として正確に表現することを考えます。(ただし、0 < p < q < 106とします。)

 このとき、結果は

      
 有限の桁で正確に表現できる。

      
ある桁の範囲を繰り返す循環小数となる。

 のいずれかとなります。筆算と同じ手順で1桁ずつ小数部を求めていくと、

      
 割り切れた(余りが 0 になった)なら、そこまでの桁で正確に表現できた。

      
 1度出てきた余りが、再び現れたなら、循環した。

 と区別できます。

 2 つの整数 p, q を入力すると、 p / q を小数で表した時の、小数部を出力するプログラムを作成してください。 ただし、

      
 結果が有限の桁で正確に表現できる時は、数値だけを 1 行に出力してください。

      
 結果が循環小数になる時は次のように 2 行に出力してください。
      

           
最初の行に、循環する部分までの数字を出力してください。

           
次の行には、循環しない部分の下には空白を出力し、循環する部分の下には「^」を出力してください。

      

      
 いずれの場合も数字列は 80 文字を超えることはないものとします。

Input

入力は複数のデータセットからなります。各データセットとして、p, q が空白区切りで１行に与えられます。データセットの数は 250 を超えません。

Output

データセットごとに、循環しない小数の場合は数値の小数部分を(この場合 1 行)、循環小数の場合は循環するまでの数字と循環する部分を示す記号「^」(この場合 2 行) を出力してください。

Sample Input

1 12
10000 32768
1 11100
1 459550

Output for the Sample Input

083
  ^
30517578125
00009
  ^^^
00000217604178
  ^^^^^^^^^^^^



Sample input:
1 12
10000 32768
1 11100
1 459550


Sample output:
083
  ^
30517578125
00009
  ^^^
00000217604178
  ^^^^^^^^^^^^

Submission status: Wrong Answer

Solution code:
while(True):
    checked = []
    try: p,q = map(int,input().split())
    except: break
    ansstr = ""
    while(True):
        p *= 10
        ans,p = p//q, p%q
        ansstr += str(ans)
        if p == 0:
            print(ansstr)
            break
        if p in checked:
            ansind = checked.index(p)
            print(ansstr)
            print(" "*ansind+"^"*(len(checked)-ansind))
            break
        else: checked.append(p)

Submission status: Wrong Answer

Solution code:
while(True):
    checked = []
    try: p,q = map(int,input().split())
    except: break
    ansstr = ""
    while(True):
        p *= 10
        ans,p = p//q, p%q
        ansstr += str(ans)
        if p == 0:
            print(ansstr)
            break
        if p in checked:
            ansind = checked.index(p)
            print(ansstr)
            print(" "*(ansind+1)+"^"*(len(checked)-ansind))
            break
        else: checked.append(p)

Submission status: Wrong Answer

Solution code:
while(True):
    checked = []
    try: p,q = map(int,input().split())
    except: break
    ansstr = ""
    p -= (p//q)*q
    while(True):
        p *= 10
        ans,p = p//q, p%q
        ansstr += str(ans)
        if p == 0:
            print(ansstr)
            break
        if p in checked:
            ansind = checked.index(p)
            print(ansstr)
            print(" "*(ansind+1)+"^"*(len(checked)-ansind))
            break
        else: checked.append(p)

Submission status: Wrong Answer

Solution code:
while(True):
    try: p,q = map(int,input().split())
    except: break
    ansstr = ""
    checked = [p%q]
    while(True):
        p *= 10
        ans,p = p//q, p%q
        if p == 0:
            print(ansstr)
            break
        ansstr += str(ans)
        if p in checked:
            ansind = checked.index(p)
            print(ansstr)
            print(" "*(ansind)+"^"*(len(checked)-ansind))
            break
        else: checked.append(p)

Submission status: Wrong Answer

Solution code:
import sys

for s in sys.stdin:
  p,q=map(int,s[:-1].split())
  a=q
  c1=0; c2=0
  while a%2==0: a/=2; c1+=1
  while a%5==0: a/=5; c2+=1
  b=9; c3=1
  while b%a!=0: b=b*10+9; c3+=1

  c=max(c1,c2)
  r1=2**(c-c1)*5**(c-c2)
  r2=b/a
  d=r1*r2*p
  s1=("0"*c+str(d/b))[-c:]
  s2=""
  if d%b!=0: s2=(c3*"0"+str(d%b))[-c3:]
  s=s1+s2
  while s[-1]=="0": s=s[:-1]
  x=(" "*len(s1)+"^"*len(s2))[-len(s):]
  print s
  print x

Submission status: Wrong Answer

Solution code:
import sys

Z="0"
for s in sys.stdin:
  p,q=map(int,s[:-1].split())
  a=q
  c1=0; c2=0
  while a%2==0: a/=2; c1+=1
  while a%5==0: a/=5; c2+=1
  c=max(c1,c2)

  b=9; c3=1
  while b%a!=0: b=b*10+9; c3+=1

  d=2**(c-c1)*5**(c-c2)*b/a*p
  s1=(c*Z+str(d/b))[-c:]
  s2=(c3*Z+str(d%b))[-c3:]
  s=s1+s2
  while s[-1]==Z: s=s[:-1]
  x=(" "*c+"^"*(len(s)-c))
  print s
  if s2!=Z: print x

Submission status: Wrong Answer

Solution code:
import sys

Z="0"
def f(a,c): return [(c*Z+str(a))[-c:],""]
for s in sys.stdin:
  p,q=map(int,s[:-1].split())
  p%=q
  a=q
  c1=0; c2=0
  while a%2==0: a/=2; c1+=1
  while a%5==0: a/=5; c2+=1
  c=max(c1,c2)
  b=9; c3=1
  while b%a!=0: b=b*10+9; c3+=1

  d=2**(c-c1)*5**(c-c2)*b/a*p
  x=d%b
  s1=f(d/b,c)[c==0]
  s2=f(x,c3)[x==0]
  s=(s1+s2).rstrip(Z)
  x=(" "*c+"^"*len(s2))[-len(s):]
  print s
  if s2!="": print x

Submission status: Wrong Answer

Solution code:
import sys

Z="0"
def f(a,c): return [(c*Z+str(a))[-c:],""]
for s in sys.stdin:
  p,q=map(int,s[:-1].split())
  p%=q
  a=q
  c1=0; c2=0
  while a%2==0: a/=2; c1+=1
  while a%5==0: a/=5; c2+=1
  c=max(c1,c2)
  b=9; c3=1
  while b%a!=0: b=b*10+9; c3+=1

  d=2**(c-c1)*5**(c-c2)*b/a*p
  x=d%b
  s1=f(d/b,c)[c==0]
  s2=f(x,c3)[x==0]
  s=(s1+s2)
  if x==0: s=s.rstrip(Z)
  x=(" "*c+"^"*len(s2))[-len(s):]
  print s
  if s2!="": print x

Submission status: Wrong Answer

Solution code:
import sys

Z="0"
def f(a,c): return [(c*Z+str(a))[-c:],""]
for s in sys.stdin:
  p,q=map(int,s[:-1].split())
  p%=q
  a=q
  c1=0; c2=0
  while a%2==0: a/=2; c1+=1
  while a%5==0: a/=5; c2+=1
  c=max(c1,c2)
  b=9; c3=1
  while b%a!=0: b=b*10+9; c3+=1

  d=2**(c-c1)*5**(c-c2)*b/a*p
  x=d%b
  s1=f(d/b,c)[c==0]
  s2=f(x,c3)[x==0]
  s=(s1+s2)
  if x==0: s=s.rstrip(Z)
  x=(" "*c+"^"*len(s2))[-len(s):]
  if s=="": s="0"
  print s
  if s2!="": print x

Submission status: Wrong Answer

Solution code:
while True:
    try:
        p, q = map(int, raw_input().split())
    except EOFError:
        break
    ans, ls = [], []
    for i in range(80):
        p = p%q*10
        ans.append(p/q)
        if p%q in ls or p%q == 0:
            break
        ls.append(p%q)
    print ''.join(map(str, ans))
    if p%q != 0:
        print ' '*(ls.index(p%q)+1)+'^'*(len(ls)-ls.index(p%q))

Submission status: Wrong Answer

Solution code:
def discomfort(r):
    if r%q == 0:
        res.append(str(r/q))
        print ''.join(res[1:])
        return True
    else:
        res.append(str(r/q))
        if len(res) >= 3:
            i = check(res[:])
            if i:
                print ''.join(res[:-i][1:])
                print ' %s ' % ('^'*(len(res[:-i][2:-1])))
                return True
        r = r-q*(r/q)
        r = int(str(r)+'0')
        if discomfort(r): return True
        


def check(c_res):
    c_res.reverse()
    for i in range(1, (len(c_res)/2)+1):
        if c_res[:i] != list('0'*len(c_res[:i])) and \
        c_res[:i][0] != '0' and \
        c_res[:i] == c_res[i:i+i]:
            return i
        
    

while True:
    try:
        p, q = map(int, raw_input().split(' '))
        res= []
        discomfort(p)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
def discomfort(r):
    if r%q == 0:
        res.append(str(r/q))
        print ''.join(res[1:])
        return True
    else:
        res.append(str(r/q))
        if len(res) >= 3:
            i = check(res[:])
            if i:
                print ''.join(res[:-i][1:])
                print ' %s ' % ('^'*(len(res[:-i][2:-1])))
                return True
        r = r-q*(r/q)
        r = int(str(r)+'0')
        if discomfort(r): return True
        


def check(c_res):
    c_res.reverse()
    for i in range(1, (len(c_res)/2)):
        if c_res[:i] != list('0'*len(c_res[:i])) and \
        c_res[:i][0] != '0' and \
        c_res[:i] == c_res[i:i+i]:
            return i
        
    

while True:
    try:
        p, q = map(int, raw_input().split(' '))
        res= []
        discomfort(p)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
def discomfort(r):
    if r%q == 0:
        res.append(str(r/q))
        print ''.join(res[1:])
        return True
    else:
        res.append(str(r/q))
        if len(res) >= 3:
            i = check(res[:])
            if i:
                print ''.join(res[:-i][1:])
                print (len(res[1:-i])-i)*' ' + i*'^'
                return True
        r = r-q*(r/q)
        r = int(str(r)+'0')
        if discomfort(r): return True
        
def check(c_res):
    c_res.reverse()
    for i in range(1, (len(c_res)/2)+1):
        if c_res[:i] != list('0'*len(c_res[:i])) and \
        c_res[:i][0] != '0' and \
        c_res[:i] == c_res[i:i+i]:
            return i

while True:
    try:
        p, q = map(int, raw_input().split(' '))
        res= []
        discomfort(p)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
def discomfort(p):
    if p%q == 0:
        ans.append(str(p/q))
        print ''.join(ans)
    else:
        ans.append(str(p/q))
        r = str(p-q*(p/q))
        if not r in r_lis:
            r_lis.append(r)
            if discomfort(int(r+'0')): return True
        else:
            i = 0
            for index, v in enumerate(r_lis):
                if r == v:
                    i = index
            print ''.join(ans[1:])
            print ' '*len(ans[1:i+1])+'^'*(len(ans[i+1:]))
            return True

while True:
    try:
        p, q = map(int, raw_input().split(' '))
        ans = []
        r_lis = []
        discomfort(p)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
while True:
	try:
		p,q = map(float,raw_input().split())
	except EOFError:
		break
	
	num = "{0:.28f}".format(p / q).replace("0.", "")
	
	start = 0
	end = 0
	array = []
	while True:
		if p in array:
			start = array.index(p)
			break
		array.append(p)
		p = p * 10 % q
		if p == 0:
			break
		end += 1
		
	print num[:end]
	if end - start != len(num[:end]):
		print " " * (len(num[:end]) - (end - start)) + "^" * (end - start)

Submission status: Wrong Answer

Solution code:
while True:
	try:
		p,q = map(float,raw_input().split())
	except EOFError:
		break
	
	num = "{0:.28f}".format(p / q).replace("0.", "")
	
	start = 0
	end = 0
	array = []
	while True:
		if p in array:
			start = array.index(p)
			break
		array.append(p)
		p = p * 10 % q
		if p == 0:
			break
		end += 1
		
	print num[:end]
	
	arrow = ""
	for i in xrange(end):
		if i < start:
			arrow += " "
		else:
			arrow += "^"
	if " " in arrow:
		print arrow

Submission status: Wrong Answer

Solution code:
import sys
def divide(p,q):
    a = p*10 // q
    b = p*10 - a*q
    arr1.append(a)
    if b == 0:
        return -1
    elif b in arr2:
        return arr2.index(b)
    else:
        arr2.append(b)
        return divide(b,q)

for line in sys.stdin:
    p,q = map(int, line.split())
    arr1 = []
    arr2 = []
    repeat = []
    r = divide(p,q)
    if r == -1:
        print(''.join(map(str, arr1)))
    else:
        for i in range(len(arr1)):
            if i <= r:
                repeat.append(' ')
            else:
                repeat.append('^')
        print(''.join(map(str, arr1)))
        print(''.join(repeat))

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxreplen = 80
    PREC=100
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(maxreplen/rep)-1)
                                         #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxreplen:
            raise ValueError('This cannot happen.')

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxreplen = 80
    PREC=200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(maxreplen/rep)-1)
                                         #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxreplen:
            raise ValueError('This cannot happen.')

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxlen = 80
#    PREC=160
    PREC=200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(PREC/rep)-1)
                                         #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.')

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxlen = 80
    PREC=200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(PREC/rep)-1)
                                         #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.')

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxlen = 85
    #PREC=200
    PREC=300
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(PREC/rep)-1)
                                         #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.')

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    #maxlen = 85
    maxlen = 100
    #PREC=200
    PREC=300
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(PREC/rep)-1)
                                         #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.')

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    #maxlen = 85
    maxlen = 160
    #PREC=200
    PREC=300
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(PREC/rep)-1)
                                         #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.')

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxlen = 80
    PREC=300
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(maxlen/rep)-1)  #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.rep=%d' % rep)

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxlen = 120
    PREC=200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(maxlen/rep)-1)  #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.rep=%d' % rep)

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

for s in sys.stdin:
    p, q = map(int, s.split())

    lst = []
    rem_lst = []
    is_devidable = False
    cycle_start_index = None

    while True:
        quot = (10 * p) // q
        rem = (10 * p) % q
        lst.append(quot)
        if rem == 0:
            is_devidable = True
            break

        if rem in rem_lst:
            cycle_start_index = rem_lst.index(rem) + 1
            break
        else:
            rem_lst.append(rem)
            p = rem

    print(''.join(str(e) for e in lst))
    if is_devidable:
        pass
    else:
        hat_num = len(lst) - cycle_start_index
        signs = ' ' * cycle_start_index + '^' * hat_num
        print(signs)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

for s in sys.stdin:
    p, q = map(int, s.split())
    pair_lst = []
    is_devidable = False
    cycle_start_index = None

    while True:
        quot = (10 * p) // q
        rem = (10 * p) % q
        pair = (quot, rem)

        if rem == 0:
            is_devidable = True
            break

        if pair in pair_lst:
            cycle_start_index = pair_lst.index(pair)
            break
        else:
            pair_lst.append(pair)
            p = rem


    if is_devidable:
        pass
    else:
        for pair in pair_lst:
            print(pair[0], end='')
        print()

        # hat
        for i, pair in enumerate(pair_lst):
            if i >= cycle_start_index:
                print('^', end='')
            else:
                print(' ', end='')
        print()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0113
"""
import sys



def solve(p, q):
    quotients = []
    mods = []
    cyclic = False
    cycle_start = 0
    cycle_length = 0

    while True:
        mods.append(p)
        p *= 10
        quotients.append(p // q)
        p = p % q

        if p == 0:
            break
        if p in mods:
            cyclic = True
            cycle_start = mods.index(p)
            cycle_length = len(quotients) - mods.index(p)
            break

    return cyclic, quotients, cycle_start, cycle_length


def main(args):
    p = 100000
    q = 32768
    cyclic, quotients, cycle_start, cycle_length = solve(p, q)
    if cyclic:
        print(''.join(map(str, quotients)))
        print(' '*cycle_start + '^'*cycle_length)
    else:
        print(''.join(map(str, quotients)))


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
while True:
    try:
        a, b = list(map(int, input().split()))
    except:
        break
    a *= 10
    done = {}
    flag = 0
    s = ""
    for i in range(80):
        tmp = ""
        while a < b:
            a *= 10
            tmp += "0"
        tmp += str(a // b)
        if a % b in done:
            flag = done[a % b]
            if len(s) < flag + 1:
                s += tmp
            break
        else:
            done[a % b] = i+2
        a = a % b * 10
        s += tmp
        if a == 0:
            break
    print(s)
    if flag:
        print(" "*flag + "^"*(len(s)-flag))

Submission status: Wrong Answer

Solution code:
while True:
    try:
        a, b = list(map(int, input().split()))
    except:
        break
    a *= 10
    done = {}
    flag = 0
    s = ""
    i = 0
    while i < 80:
        tmp = ""
        while a < b:
            a *= 10
            tmp += "0"
            i += 1
        tmp += str(a // b)
        if a % b in done:
            flag = done[a % b] - len(tmp) + 1
            if s[flag:flag+len(tmp)] != tmp:
                s += tmp
                flag += len(tmp)
            break
        else:
            done[a % b] = i
        a = a % b * 10
        s += tmp
        if a == 0:
            break
        i += 1
    print(s)
    if flag:
        print(" "*flag + "^"*(len(s)-flag))

Submission status: Accepted

Solution code:
while(True):
    try: p,q = map(int,input().split())
    except: break
    ansstr = ""
    checked = [p%q]
    while(True):
        ans,p = divmod(p*10,q)
        ansstr += str(ans)
        if p == 0:
            print(ansstr)
            break
        if p in checked:
            ansind = checked.index(p)
            print(ansstr)
            print(" "*(ansind)+"^"*(len(checked)-ansind))
            break
        else: checked.append(p)

Submission status: Accepted

Solution code:
import sys

for s in sys.stdin:
  p,q=map(int,s[:-1].split())
  F={}
  A=[]
  X=[]
  p%=q
  while 1:
    c=10*p
    if p in F or p==0: break
    a,b=divmod(c,q)
    X.append(a)
    A.append(p)
    F[p]=1
    p=b
  n=i=len(X)
  if p!=0: i=A.index(p)
  print "".join(map(str,X))
  if n!=i: print " "*i+"^"*(n-i)

Submission status: Accepted

Solution code:
import sys

for s in sys.stdin:
  p,q=map(int,s[:-1].split())
  F={}
  X=[]
  c=0
  p%=q
  while 1:
    if p in F or p==0: break
    F[p]=c
    a,p=divmod(p*10,q)
    X.append(a)
    c+=1
  F[0]=c
  i=F[p]
  print "".join(map(str,X))
  if c!=i: print " "*i+"^"*(c-i)

Submission status: Accepted

Solution code:
import sys

for s in sys.stdin:
  p,q=map(int,s[:-1].split())
  F={}
  X=[]
  c=0
  p%=q
  while not(p in F or p==0):
    F[p]=c
    a,p=divmod(p*10,q)
    X.append(a)
    c+=1
  print "".join(map(str,X))
  if p!=0: print " "*F[p]+"^"*(c-F[p])

Submission status: Accepted

Solution code:
while True:
	try:
		p, q = map(int, input().split())
		m = p % q
		i = -1
		decs = ['']
		mods = [m]
		while m != 0:
			d, m = divmod(m * 10, q)
			decs.append(str(d))
			if m not in mods:
				mods.append(m)
			else:
				i = mods.index(m)
				break
		#
		if len(decs) == 0:
			print(0)
		else:
			print(''.join(decs))
			if m != 0 and i > -1:
				for j, dec in enumerate(decs):
					if j > i:
						decs[j] = '^' * len(decs[j])
					else:
						decs[j] = ' ' * len(decs[j])
				print(''.join(decs))
	except EOFError:
		break

Submission status: Accepted

Solution code:
while True:
    try:
        p, q = map(int, raw_input().split())
    except EOFError:
        break
    ans, ls = [], [p]
    for i in range(80):
        p = p%q*10
        ans.append(p/q)
        if p%q in ls or p%q == 0:
            break
        ls.append(p%q)
    print ''.join(map(str, ans))
    if p%q != 0: print ' '*(ls.index(p%q))+'^'*(len(ls)-ls.index(p%q))

Submission status: Accepted

Solution code:
def discomfort(p):
    if p%q == 0:
        ans.append(str(p/q))
        print ''.join(ans[1:])
    else:
        ans.append(str(p/q))
        r = str(p-q*(p/q))
        if not r in r_lis:
            r_lis.append(r)
            if discomfort(int(r+'0')): return True
        else:
            i = 0
            for index, v in enumerate(r_lis):
                if r == v:
                    i = index
            print ''.join(ans[1:])
            print ' '*len(ans[1:i+1])+'^'*(len(ans[i+1:]))
            return True

while True:
    try:
        p, q = map(int, raw_input().split(' '))
        ans = []
        r_lis = []
        discomfort(p)
    except EOFError:
        break

Submission status: Accepted

Solution code:
import sys
def divide(p,q):
    a = p*10 // q
    b = p*10 - a*q
    arr1.append(a)
    if b == 0:
        return -1
    elif b in arr2:
        return arr2.index(b)
    else:
        arr2.append(b)
        return divide(b,q)

for line in sys.stdin:
    p,q = map(int, line.split())
    arr1 = []
    arr2 = [p]
    repeat = []
    r = divide(p,q)
    if r == -1:
        print(''.join(map(str, arr1)))
    else:
        for i in range(len(arr1)):
            if i < r:
                repeat.append(' ')
            else:
                repeat.append('^')
        print(''.join(map(str, arr1)))
        print(''.join(repeat))

Submission status: Accepted

Solution code:
import bisect

def isin(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect.bisect_left(a, x)
    if i != len(a):
        if a[i] == x:
            return 1
    return 0



while(1):
    try:
        p,q=map(int,raw_input().split())
        rem=[]
        rem2=[]
        ans1=''
        ans2=''
        while(1):
            if p==0:
                print ans1
                break
            elif not isin(rem,p):
                bisect.insort(rem,p)
                rem2.append(p)
                p*=10
                ans1+=str(p/q)
                p%=q
            else:
                ind=rem2.index(p)
                stl=len(ans1)
                ans2+=' '*(ind)+'^'*(stl-ind)
                print ans1
                print ans2
                break
    except:
        break

Submission status: Accepted

Solution code:
while 1:
    try:p,q=map(int,input().split())
    except:break
    a=[];b={};c=0;p%=q
    while p!=0 and p not in b:
        b[p]=c
        d,p=divmod(p*10,q)
        a+=[d];c+=1
    print(*a,sep='')
    if p!=0:print(' '*b[p]+'^'*(c-b[p]))

Submission status: Accepted

Solution code:
def solve1(m,n):
    div=[]
    rem=[]
    r=m
    rem = [-1 for _ in range(n)]
    digit=0
    while True:
        (d,r) = divmod(r,n)
        div.append(d)
        if r==0:
            return(''.join(map(str, div))[1:],'')
        elif rem[r] != -1:
            le = digit - rem[r]
            s = ''.join(map(str,div[1:]))
            t = (' '*100 + '^'*le)[-len(s):]
            return(s,t)
        else:
            rem[r] = digit
            r *= 10
            digit += 1
        
while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve1(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Accepted

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxreplen = 160
    PREC=200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(maxreplen/rep)-1)
        #r = r'(.{%d})\1{%d,}' % (rep, 20)
                                         #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxreplen:
            raise ValueError('This cannot happen.')

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Accepted

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxlen = 160
    PREC=200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(maxlen/rep)-1)  #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.rep=%d' % rep)

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Accepted

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxlen = 80
    PREC=200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(maxlen/rep))  #ex. '(.{6})\\1{12,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.rep=%d' % rep)

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break

Submission status: Accepted

Solution code:
from decimal import *
import re
def solve2(m, n):
    maxlen = 80
    PREC=200
    getcontext().prec = PREC
    x = Decimal(m) / Decimal(n)
    s = x.to_eng_string()
    if len(s) < PREC:
        return(s[2:],'')
    rep = 1
    while True:
        r = r'(.{%d})\1{%d,}' % (rep, int(maxlen/rep))  #ex. '(.{6})\\1{13,}'
        a=re.search(r, s)
        if a:
            break
        rep += 1
        if rep > maxlen:
            raise ValueError('This cannot happen.rep=%d' % rep)

    u = s[2:a.start()+len(a.group(1))]
    v = (' '*PREC + '^'*len(a.group(1)))[-len(u):]
    return(u,v)

while True:
    try:
        m,n = map(int, input().strip().split())
        s,t = solve2(m, n)
        print(s)
        if t!='':
            print(t)
    except EOFError:
        break