fork download
  1. from sys import stdout
  2. import os
  3. import io
  4. from collections import deque
  5.  
  6. input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
  7.  
  8. def convert (n, base):
  9. if n == 0:
  10. return '0'
  11. nums = []
  12. while n:
  13. n, r = divmod(n, base)
  14. nums.append(str(r))
  15. nums.reverse()
  16. return ''.join(nums)
  17.  
  18. t: int = int(input())
  19.  
  20. while t:
  21. t -= 1
  22. p, n = [int(i) for i in input().split()]
  23. result = 0
  24. for ch in convert(n, p):
  25. result += int(ch)
  26.  
  27. stdout.write(f"{result}\n")
Success #stdin #stdout 0.03s 9832KB
stdin
4
6 156
99 1666
1000 2626262
9 12421421
stdout
6
17
26
37