Statistics

Problem Statement for "CatAndRat"

Problem Statement

We have a circular tube. At one point the tube has an entrance.

At time 0, a rat enters the tube via the entrance. The rat can move inside the tube in both directions (clockwise and counterclockwise). The rat can change direction in an instant. Its maximum speed is Vrat.

At time T, the cat will enter the tube via the same entrance. The cat can also move in both directions and change its direction instantly. Its maximum speed is Vcat.

For the purpose of this problem, you should imagine the tube as a circle with radius R, and the rat and the cat as points on said circle.

The cat is trying to catch the rat as quickly as possible. The rat is trying not to be caught, and if that is impossible, to be caught as late as possible. At any time (starting at time 0) the cat and the rat know each other's position. Already at time 0 the rat knows the time T. Assume that both the cat and the rat behave optimally.

You are given the ints R, T, Vrat, and Vcat. If the cat will catch the rat successfully, return how much time it'll take the cat to catch the rat. Otherwise, return -1.0.

Definition

Class:
CatAndRat
Method:
getTime
Parameters:
int, int, int, int
Returns:
double
Method signature:
double getTime(int R, int T, int Vrat, int Vcat)
(be sure your method is public)

Notes

  • The cat and rat cannot leave the tube.
  • Your return value must have a relative or absolute error less than 1e-9.

Constraints

  • R, T, Vrat, and Vcat will each be between 1 and 1000, inclusive.

Examples

  1. 10

    1

    1

    1

    Returns: -1.0

    The rat can survive. During the first unit of time it has to run away from the entrance. Then, once the cat enters the tube, the rat can always run in the same direction and with the same speed as the cat.

  2. 10

    1

    1

    2

    Returns: 1.0

    The cat is now faster than the rat. The best strategy for the rat is to enter the tube and to start running in either direction at its maximum speed. The cat will then enter the tube, run in the same direction as the rat, and catch it in exactly 1 unit of time.

  3. 10

    1

    2

    1

    Returns: -1.0

    The rat is now faster than the cat. It can survive, for example by using the strategy described in Example 0.

  4. 1000

    1000

    1

    1000

    Returns: 1.001001001001001

  5. 1

    1000

    1

    2

    Returns: 3.141592653589793

  6. 1000

    1000

    999

    1000

    Returns: 3141.592653589793

  7. 1000

    1000

    1

    1000

    Returns: 1.001001001001001

  8. 1

    1000

    1

    1000

    Returns: 0.0031447373909807737

  9. 1

    1

    1

    1000

    Returns: 0.001001001001001001

  10. 1000

    1

    1

    1000

    Returns: 0.001001001001001001

  11. 1

    1

    1000

    999

    Returns: -1.0

  12. 1

    1

    1000

    1000

    Returns: -1.0

  13. 1000

    1000

    3

    4

    Returns: 3000.0

  14. 1000

    1000

    4

    5

    Returns: 3141.592653589793

  15. 1000

    1000

    1000

    1000

    Returns: -1.0

  16. 1000

    1000

    1000

    1

    Returns: -1.0

  17. 109

    24

    345

    435

    Returns: 3.804817769347638

  18. 234

    54

    23

    45

    Returns: 33.415121860909615

  19. 100

    1

    314

    315

    Returns: 314.0

  20. 100

    1

    315

    316

    Returns: 314.1592653589793

  21. 1

    1

    1

    1

    Returns: -1.0

  22. 10

    50

    1

    2

    Returns: 31.41592653589793

  23. 2

    2

    4

    5

    Returns: 6.283185307179586

  24. 1

    1000

    34

    989

    Returns: 0.00328962581527727

  25. 122

    598

    2

    100

    Returns: 3.9109622830403548

  26. 3

    3

    4

    5

    Returns: 9.42477796076938

  27. 1

    1

    998

    1000

    Returns: 1.5707963267948966

  28. 10

    7

    10

    20

    Returns: 3.141592653589793

  29. 3

    3

    3

    4

    Returns: 9.0

  30. 1

    5

    1

    2

    Returns: 3.141592653589793

  31. 1000

    1

    1

    2

    Returns: 1.0

  32. 1

    2

    1

    1000

    Returns: 0.002002002002002002

  33. 1

    1000

    5

    7

    Returns: 1.5707963267948966

  34. 6

    6

    5

    6

    Returns: 18.84955592153876

  35. 1

    1000

    2

    3

    Returns: 3.141592653589793

  36. 10

    1

    40

    50

    Returns: 3.141592653589793

  37. 10

    100

    3

    5

    Returns: 15.707963267948966

  38. 100

    314

    1

    2

    Returns: 314.0

  39. 10

    16

    2

    3

    Returns: 31.41592653589793

  40. 3

    17

    1

    2

    Returns: 9.42477796076938

  41. 10

    35

    1

    2

    Returns: 31.41592653589793

  42. 100

    23

    2

    3

    Returns: 46.0

  43. 50

    10

    6

    10

    Returns: 15.0

  44. 10

    17

    2

    100

    Returns: 0.320570678937734

  45. 5

    10

    3

    4

    Returns: 15.707963267948966


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: