06_boatrace.py 753 B

1234567891011121314151617181920
  1. #!/usr/bin/env python3
  2. import sys
  3. import re
  4. from functools import reduce
  5. from itertools import starmap
  6. from math import ceil, sqrt
  7. from operator import mul
  8. def wins(time, distance):
  9. # invariant: hold * (time - hold) > distance
  10. # solve: -hold^2 + time*hold - distance = 0
  11. # hold = (time +- sqrt(time^2 - 4 * distance)) / 2
  12. # = (time +- d) / 2
  13. # solution: (time - d) / 2 < hold < (time - d) / 2
  14. d = sqrt(time ** 2 - 4 * distance)
  15. return int((time + d) / 2) - ceil((time - d) / 2) + 1 - 2 * (d % 1 == 0)
  16. times, distances = (re.findall(r'\d+', line) for line in sys.stdin)
  17. print(reduce(mul, starmap(wins, zip(map(int, times), map(int, distances)))))
  18. print(wins(int(''.join(times)), int(''.join(distances))))