fork(1) download
  1. dig = {}
  2. dig[1] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  3. dig[3] = [0, 7, 4, 1, 8, 5, 2, 9, 6, 3]
  4. dig[7] = [0, 3, 6, 9, 2, 5, 8, 1, 4, 7]
  5. dig[9] = [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
  6.  
  7. # dig[x][y] gives the digit z such that x * z = p, where p has last digit y
  8.  
  9. def solve(n, x):
  10. x0 = x % 10
  11. y = dig[x0][1]
  12. pow10 = 10
  13. for i in range(1, n):
  14. curdig = ((x * y) // pow10) % 10 # current ith digit of the product
  15. reqdig = (10 - curdig) % 10 # the digit we want to add to the current digit to get 0
  16. ydig = dig[x0][reqdig] # the next digit of y we need to get the required digit
  17. y = ydig * pow10 + y # update y
  18. pow10 *= 10
  19. return y
  20.  
  21. for t in range(int(input())):
  22. n, x = map(int, input().split())
  23. print(solve(n, x))
Success #stdin #stdout 0.01s 27720KB
stdin
6
1 9
1 7
2 11
3 17
5 11327
5 113
stdout
9
3
91
353
23263
60177