AnupamRoy's blog

By AnupamRoy, history, 4 months ago, In English

for avobe problem as editorial the solution is here . but it gives worng answer what is the problem in my python code

for i in range(int(input())): n,m=map(int,input().split()) p=0 while 1: if n%m==0: n/=m n=int(n) p+=1 else: r=(n%m) n-=r p+=r if n==0: break print(p)

  • Vote: I like it
  • -8
  • Vote: I do not like it

4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

n /= m should be n //= m.

This is because the / operator in python is floating point division and using this converts your numbers to floats. Floating point numbers don't have enough precision to store big integers. // is integer division, which is precise and that is what you want.