Statistics

Problem Statement for "BusTravel"

Problem Statement

There are N cities in the country. They are numbered from 0 to N-1, inclusive.


We want to organize an onsite programming competition. We have an external sponsor who will pay for S contest sites. We can pick any S distinct cities for these contest sites.

We would like to make it so that students from all cities can take part in the contest. After we choose the contest sites, we will rent some buses to help all other students reach one of the contest sites.


The buses available for rent are large enough: all contestants from the whole country will fit into any single bus.

We will organize a sequence of exactly B = N - S buses. The buses will be numbered from 1 to B, inclusive. Each bus will travel from one city to another city. For each x > 1, bus x will only depart from its starting city after bus x-1 arrives to its destination.

You are free to choose the source and destination for each bus. In particular, the destination of a bus can be any city, not just a contest site. In order to reach a contest site, some students may need to travel via other cities, taking multiple buses.

The cost of booking a bus depends not only on its route but also on the time of day. Having bus number x travel from city y to city z costs (x*(y+4)*(y+z+A) modulo M)+1 coins.


Calculate and return the minimum total cost of getting students from the whole country to the contest sites, given that we can choose where to have the contest sites and also the sequence of buses to get the rest of the country to the contest.

Definition

Class:
BusTravel
Method:
optimize
Parameters:
int, int, int, int
Returns:
int
Method signature:
int optimize(int N, int S, int M, int A)
(be sure your method is public)

Notes

  • There is nothing specific in the formula for bus rental costs. The reference solution does not depend on any special property of the costs.
  • You must book exactly B = N - S buses. (There may be cheaper solutions with more buses, but for logistical reasons we cannot use them.)

Constraints

  • N will be between 1 and 16, inclusive.
  • S will be between 1 and N, inclusive.
  • M will be between 1 and 1,000, inclusive.
  • A will be between 0 and M-1, inclusive.

Examples

  1. 4

    4

    47

    7

    Returns: 0

    We can make a contest site in every city. No money for buses needed!

  2. 4

    3

    47

    7

    Returns: 4

    For reference, all possible bus costs are below. (In the table, row x column y has the cost of booking the bus from city x to city y.) Bus 1: -- 33 37 41 41 -- 4 9 8 14 -- 26 24 31 38 -- The cheapest bus we can book is from city 1 to city 2, the cost is 4 coins. The optimal solution is to book this bus and to make contest sites in cities 0, 2, 3.

  3. 4

    2

    47

    7

    Returns: 8

    Costs for bus #1 are the same as in the previous example. Costs for bus 2 are as follows: Bus 2: -- 18 26 34 34 -- 7 17 15 27 -- 4 47 14 28 -- The optimal solution here looks as follows: Bus #1 is booked from city 1 to city 2. Its cost is 4. Bus #2 is booked from city 2 to city 3. Its cost is also 4. The contest sites will be in cities 0 and 3. Students from city 1 will take both buses to get to city 3. Students from city 2 will take bus #2 to get to city 3.

  4. 4

    1

    47

    7

    Returns: 25

    Bus 3: -- 3 15 27 27 -- 10 25 22 40 -- 29 23 44 18 -- Here we need to get everyone to the same city. The optimal solution differs from the previous one quite significantly: Bus #1 is booked from city 2 to city 0. Its cost is 8. Bus #2 is booked from city 3 to city 1. Its cost is 14. Bus #3 is booked from city 0 to city 1. Its cost is 3. The only contest site will be in city 1. We can easily verify that students from all other cities can reach it using some of the buses we booked. Note that taking the optimal solution for the previous example and then booking bus #3 as in this solution is NOT a valid solution. Remember that buses are numbered in chronological order. If we have buses 1->2, 2->3, and only then 0->1, students from city 0 will not be able to reach city 3.

  5. 4

    1

    47

    17

    Returns: 30

    Different A gives us different bus rental costs and leads to a different solution.

  6. 14

    4

    1

    0

    Returns: 10

    Each bus can be booked for one coin. We can, for example, book buses from cities 0 through 9 to city 12, and then organize contest sites in cities 10 through 13.

  7. 1

    1

    1

    0

    Returns: 0

  8. 1

    1

    42

    23

    Returns: 0

  9. 2

    1

    1

    0

    Returns: 1

  10. 2

    1

    42

    23

    Returns: 13

  11. 13

    13

    42

    23

    Returns: 0

  12. 16

    16

    7

    3

    Returns: 0

  13. 8

    1

    244

    146

    Returns: 72

  14. 8

    3

    228

    157

    Returns: 29

  15. 3

    2

    2

    0

    Returns: 1

  16. 7

    4

    3

    2

    Returns: 3

  17. 9

    4

    27

    14

    Returns: 6

  18. 6

    1

    109

    76

    Returns: 45

  19. 8

    5

    23

    1

    Returns: 6

  20. 5

    3

    6

    4

    Returns: 2

  21. 9

    5

    5

    2

    Returns: 4

  22. 6

    4

    7

    4

    Returns: 2

  23. 8

    6

    47

    35

    Returns: 4

  24. 7

    3

    3

    1

    Returns: 4

  25. 12

    7

    46

    38

    Returns: 6

  26. 4

    3

    28

    15

    Returns: 1

  27. 7

    5

    15

    0

    Returns: 2

  28. 5

    1

    6

    4

    Returns: 4

  29. 12

    9

    479

    187

    Returns: 15

  30. 15

    1

    62

    58

    Returns: 34

  31. 14

    11

    66

    31

    Returns: 3

  32. 6

    3

    175

    45

    Returns: 16

  33. 10

    4

    9

    2

    Returns: 6

  34. 14

    13

    114

    13

    Returns: 1

  35. 7

    4

    2

    1

    Returns: 3

  36. 4

    2

    3

    0

    Returns: 2

  37. 5

    2

    358

    253

    Returns: 47

  38. 11

    8

    2

    0

    Returns: 3

  39. 5

    3

    2

    0

    Returns: 2

  40. 9

    2

    2

    1

    Returns: 7

  41. 3

    2

    20

    17

    Returns: 1

  42. 12

    5

    7

    2

    Returns: 7

  43. 11

    1

    7

    3

    Returns: 11

  44. 8

    3

    327

    32

    Returns: 38

  45. 11

    2

    488

    421

    Returns: 56

  46. 3

    2

    6

    5

    Returns: 1

  47. 10

    9

    5

    2

    Returns: 1

  48. 15

    6

    15

    0

    Returns: 9

  49. 12

    2

    491

    141

    Returns: 119

  50. 13

    9

    2

    1

    Returns: 4

  51. 3

    2

    452

    35

    Returns: 145

  52. 14

    13

    7

    4

    Returns: 1

  53. 16

    1

    103

    52

    Returns: 40

  54. 16

    1

    18

    3

    Returns: 15

  55. 16

    1

    57

    21

    Returns: 24

  56. 16

    2

    77

    11

    Returns: 22

  57. 16

    2

    220

    12

    Returns: 34

  58. 16

    2

    71

    50

    Returns: 24

  59. 16

    3

    48

    13

    Returns: 13

  60. 16

    3

    45

    29

    Returns: 13

  61. 16

    3

    60

    59

    Returns: 13

  62. 16

    4

    418

    345

    Returns: 51

  63. 16

    4

    2

    1

    Returns: 12

  64. 16

    4

    2

    1

    Returns: 12

  65. 16

    5

    480

    314

    Returns: 17

  66. 16

    5

    50

    6

    Returns: 11

  67. 16

    5

    39

    10

    Returns: 14

  68. 16

    6

    214

    127

    Returns: 38

  69. 16

    6

    188

    128

    Returns: 14

  70. 16

    6

    52

    35

    Returns: 10

  71. 16

    7

    3

    2

    Returns: 9

  72. 16

    7

    347

    193

    Returns: 33

  73. 16

    7

    2

    0

    Returns: 9

  74. 16

    8

    7

    3

    Returns: 8

  75. 16

    8

    2

    1

    Returns: 8

  76. 16

    8

    14

    0

    Returns: 8

  77. 16

    9

    9

    8

    Returns: 7

  78. 16

    9

    128

    98

    Returns: 9

  79. 16

    9

    489

    322

    Returns: 30

  80. 16

    10

    79

    65

    Returns: 6

  81. 16

    10

    154

    22

    Returns: 11

  82. 16

    10

    233

    227

    Returns: 9

  83. 16

    11

    5

    3

    Returns: 5

  84. 16

    11

    480

    305

    Returns: 5

  85. 16

    11

    7

    4

    Returns: 5

  86. 16

    12

    119

    95

    Returns: 4

  87. 16

    12

    26

    12

    Returns: 4

  88. 16

    12

    5

    3

    Returns: 4

  89. 16

    13

    77

    32

    Returns: 3

  90. 16

    13

    303

    182

    Returns: 3

  91. 16

    13

    3

    2

    Returns: 3

  92. 16

    14

    5

    4

    Returns: 2

  93. 16

    14

    24

    22

    Returns: 2

  94. 16

    14

    39

    35

    Returns: 2

  95. 16

    15

    3

    2

    Returns: 1

  96. 16

    15

    15

    11

    Returns: 1

  97. 16

    15

    28

    19

    Returns: 1

  98. 16

    1

    945

    64

    Returns: 87

  99. 16

    1

    955

    566

    Returns: 103

  100. 16

    1

    958

    584

    Returns: 201

  101. 16

    1

    943

    263

    Returns: 209

  102. 16

    1

    965

    395

    Returns: 320

  103. 16

    1

    950

    630

    Returns: 182

  104. 16

    1

    905

    814

    Returns: 209

  105. 16

    1

    953

    674

    Returns: 160

  106. 16

    1

    903

    104

    Returns: 163

  107. 16

    1

    900

    270

    Returns: 166

  108. 16

    1

    961

    741

    Returns: 186

  109. 16

    1

    969

    9

    Returns: 251

  110. 16

    1

    940

    581

    Returns: 235

  111. 16

    1

    954

    235

    Returns: 233

  112. 16

    1

    930

    535

    Returns: 217

  113. 5

    4

    3

    2

    Returns: 1


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: