Statistics

Problem Statement for "VisitEdges"

Problem Statement

Time limit: 4 seconds.


The infinite grid graph is a graph whose vertices are all pairs of integers, and two vertices (x1,y1) and (x2,y2) are connected by an edge if and only if abs(x1-x2) + abs(y1-y2) = 1.

We can visualize such a graph as follows: Vertices are points in a horizontal plane with integer coordinates. We can draw two lines through each point, each of them parallel to one of the coordinate axes. For each of these lines, each pair of consecutive vertices on the line is connected by an edge.


You start a random walk in the vertex (0, 0).

In each step of the walk you traverse one of the four edges that lead from your current vertex. These four edges point north (y increases), south (y decreases), east (x increases), and west (x decreases). You are given the ints N, S, E, and W: the relative probabilities of moving north, south, east and west. These probabilities remain the same during the entire walk.

Your walk terminates as soon as you traverse the M-th distinct edge.


Calculate and return the expected number of steps in your random walk.

Definition

Class:
VisitEdges
Method:
solve
Parameters:
int, int, int, int, int
Returns:
double
Method signature:
double solve(int M, int N, int S, int E, int W)
(be sure your method is public)

Notes

  • The meaning of "relative probabilities" is as follows: the probability of moving north in any step is N / (N + S + E + V).
  • Return values with an absolute or a relative error at most 1e-9 will be accepted.

Constraints

  • M will be between 1 and 7, inclusive.
  • N, S, E, and W will each be between 0 and 10, inclusive.
  • At least one of N, S, E, and W will be positive.

Examples

  1. 3

    1

    0

    0

    0

    Returns: 3.0

    In each step we go north. In three steps we are guaranteed to traverse three distinct edges.

  2. 3

    1

    1

    0

    0

    Returns: 6.0

    In each step we flip a fair coin whether we go north or south. In this setting visiting three distinct edges might take longer, as, for example, we can start by going north, south, and north again. We just performed three steps but we are still not done: we have still only traversed one edge (three times).

  3. 3

    1

    1

    1

    1

    Returns: 3.761904761904762

    Perhaps paradoxically, adding even more possible directions now somewhat decreases the expected number of steps.

  4. 1

    4

    5

    6

    7

    Returns: 1.0

    Regardless of the probabilities, in the first step we are guaranteed to traverse a new edge.

  5. 4

    4

    5

    6

    7

    Returns: 5.276493355280059

  6. 7

    4

    5

    6

    7

    Returns: 9.949621352068304

  7. 7

    4

    0

    0

    7

    Returns: 7.0

  8. 1

    0

    0

    7

    0

    Returns: 1.0

  9. 4

    0

    1

    2

    1

    Returns: 5.2436509664841555

  10. 6

    1

    1

    2

    1

    Returns: 8.111115394357354

  11. 5

    2

    0

    1

    2

    Returns: 5.868050513099962

  12. 2

    0

    0

    1

    1

    Returns: 3.0

  13. 7

    2

    0

    1

    2

    Returns: 8.293464333680454

  14. 5

    0

    0

    2

    2

    Returns: 14.999999999999996

  15. 4

    1

    0

    0

    2

    Returns: 4.0

  16. 4

    2

    2

    1

    2

    Returns: 5.191805258407552

  17. 4

    2

    2

    0

    0

    Returns: 10.0

  18. 7

    0

    0

    1

    0

    Returns: 7.0

  19. 4

    9

    6

    8

    8

    Returns: 5.196354571557175

  20. 4

    1

    10

    3

    5

    Returns: 4.553285259815844

  21. 3

    7

    1

    6

    9

    Returns: 3.7078984587219317

  22. 4

    10

    4

    3

    8

    Returns: 4.924524740498578

  23. 4

    5

    8

    9

    0

    Returns: 4.680895267085048

  24. 5

    4

    9

    6

    4

    Returns: 6.488535126184254

  25. 7

    8

    3

    9

    1

    Returns: 8.245075183695173

  26. 7

    3

    6

    7

    4

    Returns: 9.439795965854538

  27. 6

    8

    2

    7

    0

    Returns: 6.687840680529059

  28. 3

    8

    10

    1

    1

    Returns: 4.854158551930306

  29. 4

    8

    9

    8

    7

    Returns: 5.228677567248667

  30. 5

    5

    0

    9

    4

    Returns: 6.3832568134466605

  31. 7

    9

    8

    10

    7

    Returns: 9.76811635255514

  32. 7

    10

    5

    0

    9

    Returns: 8.443774350734289

  33. 3

    3

    5

    6

    2

    Returns: 3.6124679165830487

  34. 3

    7

    3

    2

    1

    Returns: 3.920003384726102

  35. 3

    9

    5

    8

    6

    Returns: 3.711703534171559

  36. 2

    4

    7

    0

    10

    Returns: 2.1711057304277643

  37. 2

    6

    1

    5

    0

    Returns: 2.11231884057971

  38. 4

    0

    2

    2

    8

    Returns: 5.0752121287277525

  39. 7

    4

    7

    0

    0

    Returns: 19.82927822797933

  40. 7

    7

    8

    9

    7

    Returns: 9.809569701609018

  41. 7

    1

    2

    3

    4

    Returns: 10.307598813453723


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: