Statistics

Problem Statement for "BackyardTrees"

Problem Statement

This problem statement contains images that may not display properly if viewed outside of the applet.

Your backyard is a rectangular grid that measures width x height square meters. You would like to plant treeCount trees using the following rules:
- All trees must be planted at integer coordinates on the grid.
- All trees must lie on the same straight line.
- Each pair of trees must be at least distance meters away from each other.

For example, two (of many) ways in which four trees could be planted on a 10x10 grid if distance is 2 are depicted below:

   

Return the number of distinct ways in which the trees could be planted, modulo 1,000,000,000. Two layouts are considered distinct if there exists a point (x, y) such that one layout contains a tree at (x, y) and the other layout does not.

Definition

Class:
BackyardTrees
Method:
countWays
Parameters:
int, int, int, int
Returns:
int
Method signature:
int countWays(int treeCount, int width, int height, int distance)
(be sure your method is public)

Constraints

  • treeCount will be between 1 and 50, inclusive.
  • width will be between 1 and 500, inclusive.
  • height will be between 1 and 500, inclusive.
  • distance will be between 1 and 50, inclusive.

Examples

  1. 2

    4

    4

    1

    Returns: 300

    There are only two trees, and the distance between any two points with integer coordinates is always at least 1. Therefore, you can place the two trees at any two points with integer coordinates. There are 25 such points and this gives you 300 different ways to plant the trees.

  2. 13

    36

    48

    5

    Returns: 2

    The diagonal of the backyard has a length of 60, which is just enough to place 13 trees with a distance of 5 between each adjacent pair. Luckily, these 13 points are at integer coordinates, so you can place the trees along either of the two diagonals while satisfying all the rules.

  3. 5

    5

    5

    1

    Returns: 88

  4. 50

    49

    49

    1

    Returns: 102

    You can place the trees along the rows or the columns of the grid, as well as on the two diagonals.

  5. 6

    5

    5

    2

    Returns: 0

    You can't plant 6 trees on the same line with the necessary distance between them on this grid.

  6. 10

    55

    75

    5

    Returns: 490260662

  7. 1

    7

    8

    50

    Returns: 72

  8. 2

    2

    36

    14

    Returns: 2484

  9. 3

    417

    232

    43

    Returns: 246502024

  10. 4

    39

    28

    3

    Returns: 2640350

  11. 5

    40

    28

    10

    Returns: 597

  12. 6

    40

    48

    6

    Returns: 7956408

  13. 7

    300

    326

    5

    Returns: 231751905

  14. 2

    1

    1

    1

    Returns: 6

  15. 8

    496

    438

    39

    Returns: 446208395

  16. 9

    2

    36

    4

    Returns: 2145

  17. 10

    317

    82

    15

    Returns: 234329616

  18. 11

    39

    28

    4

    Returns: 0

  19. 12

    40

    28

    4

    Returns: 0

  20. 13

    40

    48

    2

    Returns: 227955713

  21. 14

    250

    326

    24

    Returns: 457474184

  22. 15

    388

    163

    15

    Returns: 994478308

  23. 16

    500

    48

    11

    Returns: 206739693

  24. 17

    494

    14

    23

    Returns: 611184520

  25. 18

    60

    58

    2

    Returns: 172610472

  26. 19

    2

    58

    4

    Returns: 0

  27. 20

    222

    105

    10

    Returns: 585030272

  28. 21

    458

    14

    21

    Returns: 822102050

  29. 22

    16

    42

    1

    Returns: 841191620

  30. 23

    228

    500

    4

    Returns: 805903416

  31. 24

    91

    500

    7

    Returns: 872481282

  32. 25

    421

    312

    11

    Returns: 424493976

  33. 26

    289

    464

    14

    Returns: 175093450

  34. 27

    467

    470

    15

    Returns: 909537996

  35. 28

    273

    23

    3

    Returns: 451361080

  36. 29

    427

    3

    2

    Returns: 959744000

  37. 30

    306

    328

    3

    Returns: 385393404

  38. 31

    111

    252

    5

    Returns: 378222252

  39. 32

    193

    365

    3

    Returns: 943182664

  40. 33

    139

    447

    6

    Returns: 835091520

  41. 34

    223

    207

    6

    Returns: 337796324

  42. 35

    225

    500

    8

    Returns: 490522768

  43. 36

    31

    165

    3

    Returns: 53515776

  44. 37

    343

    403

    9

    Returns: 517677600

  45. 38

    478

    451

    15

    Returns: 483173738

  46. 39

    409

    154

    2

    Returns: 238660604

  47. 40

    268

    477

    4

    Returns: 371559939

  48. 41

    268

    83

    5

    Returns: 644090076

  49. 42

    453

    500

    4

    Returns: 272105444

  50. 43

    241

    461

    11

    Returns: 284114760

  51. 44

    450

    488

    6

    Returns: 325459699

  52. 45

    494

    59

    9

    Returns: 531469792

  53. 46

    209

    500

    4

    Returns: 73027340

  54. 47

    390

    500

    10

    Returns: 844183312

  55. 48

    500

    500

    13

    Returns: 776440460

  56. 49

    107

    5

    2

    Returns: 200751800

  57. 50

    370

    455

    9

    Returns: 918208088

  58. 50

    439

    500

    1

    Returns: 892203494

  59. 50

    126

    494

    7

    Returns: 176144876

  60. 50

    76

    181

    3

    Returns: 935538828

  61. 50

    455

    447

    12

    Returns: 599741912

  62. 50

    312

    37

    4

    Returns: 370205520

  63. 50

    430

    154

    6

    Returns: 788385595

  64. 50

    500

    240

    5

    Returns: 105571448

  65. 50

    13

    499

    9

    Returns: 230426224

  66. 50

    455

    143

    5

    Returns: 52964602

  67. 50

    99

    84

    2

    Returns: 4479

  68. 50

    500

    500

    12

    Returns: 17751284

  69. 50

    500

    500

    13

    Returns: 920638408

  70. 50

    500

    500

    14

    Returns: 920625772

  71. 50

    500

    500

    15

    Returns: 0

  72. 50

    500

    500

    16

    Returns: 0

  73. 30

    500

    500

    23

    Returns: 29891264

  74. 30

    500

    500

    24

    Returns: 29890080

  75. 30

    500

    500

    25

    Returns: 0

  76. 15

    500

    500

    47

    Returns: 126734328

  77. 15

    500

    500

    49

    Returns: 14709420

  78. 15

    500

    500

    50

    Returns: 0

  79. 2

    2

    2

    2

    Returns: 16

  80. 2

    500

    500

    2

    Returns: 499624500

  81. 1

    5

    5

    1

    Returns: 36

  82. 1

    251

    497

    2

    Returns: 125496

  83. 1

    500

    500

    50

    Returns: 251001

  84. 50

    500

    500

    2

    Returns: 911709884

  85. 1

    2

    2

    1

    Returns: 9

  86. 30

    500

    500

    27

    Returns: 0


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: