Guest User

Codejam1a

a guest
Apr 10th, 2020
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import threading
  2. def numAt(a, b):
  3. if(a == 1 or b ==1 ):
  4. return 1
  5. elif(a == b):
  6. return 1
  7. else:
  8. return numAt(a-1,b-1)+numAt(a-1,b)
  9.  
  10. def printTuple(a):
  11. first = str(a[0])
  12. second = str(a[1])
  13. print(first + ' ' + second)
  14.  
  15. def printTuples(a):
  16. for b in a:
  17. printTuple(b)
  18.  
  19. def debugSumTuples(a):
  20. sum = 0
  21. for i in a:
  22. sum+=addTuple(i)
  23. return sum
  24.  
  25. def addTuple(a):
  26. return numAt(a[0],a[1])
  27.  
  28. def solve(target, line):
  29. sum = line - 1
  30. lastOfLine = 0
  31. remainder = 0
  32. for i in range (1, line+1):
  33. oldsum = sum
  34. sum+= numAt(line,i)
  35. if sum == target:
  36. lastOfLine=i
  37. remainder = 0
  38. break
  39. if sum>target:
  40. lastOfLine = i-1
  41. remainder = target - oldsum
  42. if(remainder > line):
  43. return solve(target,line+1)
  44. break
  45. returnList = [(1,1)]
  46. for i in range (2, line):
  47. if i-1 == remainder and remainder != 0:
  48. returnList.append((i,2))
  49. returnList.append((i,1))
  50. for i in range (1, lastOfLine+1):
  51. returnList.append((line,i))
  52. return returnList
  53.  
  54. numtests = input()
  55. for i in range(1, int(numtests) + 1):
  56. num = int(input())
  57. firstTuple = (1, 1)
  58. listTuples = []
  59. listTuples.append(firstTuple)
  60. if(num == 1):
  61. print("Case #" + str(i) + ":")
  62. printTuples(listTuples)
  63. else:
  64. sum = 1
  65. for j in range(2, 500):
  66. hypothetical = sum + 2**(j-1)
  67. if hypothetical > num:
  68. newList = solve(num, j)
  69. print("Case #" + str(i) + ":")
  70. printTuples(newList)
  71. #print("Sum: " + str(debugSumTuples(newList)))
  72. break
  73. else:
  74. sum+=1
Advertisement
Add Comment
Please, Sign In to add comment