Statistics

Problem Statement for "ExactTree"

Problem Statement

In this problem, if a tree has x nodes, the nodes are numbered 0 through x-1. Given a tree T, the distance dist(i,j) between nodes i and j is the number of edges on the unique simple path that connects them. The sum of all pairwise distances in T, denoted S(T), is the sum of dist(i,j) over all pairs i,j such that i<j.

You are given ints n, m, and r. We are interested in trees with the following properties:
  • The tree T has exactly n nodes (labeled 0 through n-1).
  • S(T) modulo m equals r.

If there are such trees, return the smallest possible value S(T). Otherwise, return -1.

Definition

Class:
ExactTree
Method:
getTree
Parameters:
int, int, int
Returns:
int
Method signature:
int getTree(int n, int m, int r)
(be sure your method is public)

Constraints

  • n will be between 2 and 50, inclusive.
  • m will be between 2 and 100, inclusive.
  • r will be between 0 and m-1, inclusive.

Examples

  1. 4

    100

    9

    Returns: 9

    For a tree T on 4 vertices there are only two possible values of S(T): 9 and 10. (One tree that has S(T)=9 is a tree with the edges 0-1, 0-2, and 0-3. One tree that has S(T)=10 is a tree with the edges 0-1, 1-2, and 2-3.) We are only interested in trees T such that S(T) modulo 100 = 9. Given this constraint, the smallest actually possible value S(T) is 9.

  2. 4

    100

    10

    Returns: 10

  3. 4

    100

    0

    Returns: -1

  4. 6

    2

    0

    Returns: 28

  5. 6

    2

    1

    Returns: 25

  6. 25

    100

    0

    Returns: 700

  7. 50

    97

    89

    Returns: 2708

  8. 17

    85

    14

    Returns: 354

  9. 49

    27

    24

    Returns: 2616

  10. 30

    74

    12

    Returns: 974

  11. 19

    89

    40

    Returns: 396

  12. 42

    11

    5

    Returns: 1798

  13. 4

    46

    35

    Returns: -1

  14. 40

    92

    83

    Returns: 1739

  15. 13

    18

    12

    Returns: 174

  16. 14

    6

    3

    Returns: 189

  17. 9

    23

    10

    Returns: 102

  18. 44

    36

    17

    Returns: 2177

  19. 50

    29

    27

    Returns: 2724

  20. 26

    88

    19

    Returns: 811

  21. 43

    63

    9

    Returns: 1962

  22. 44

    89

    71

    Returns: 2118

  23. 2

    92

    14

    Returns: -1

  24. 28

    48

    16

    Returns: 928

  25. 46

    58

    25

    Returns: 2345

  26. 3

    38

    9

    Returns: -1

  27. 4

    68

    58

    Returns: -1

  28. 21

    60

    49

    Returns: -1

  29. 16

    36

    3

    Returns: 291

  30. 21

    13

    8

    Returns: 502

  31. 9

    94

    25

    Returns: -1

  32. 36

    2

    1

    Returns: 1225

  33. 19

    13

    11

    Returns: 388

  34. 35

    55

    44

    Returns: 1474

  35. 43

    39

    21

    Returns: 2088

  36. 20

    39

    37

    Returns: 427

  37. 14

    69

    48

    Returns: 255

  38. 37

    39

    30

    Returns: 1512

  39. 8

    29

    20

    Returns: 49

  40. 23

    79

    3

    Returns: 714

  41. 31

    10

    7

    Returns: -1

  42. 31

    97

    38

    Returns: 1008

  43. 4

    55

    31

    Returns: -1

  44. 44

    21

    18

    Returns: 1929

  45. 9

    11

    10

    Returns: 76

  46. 33

    80

    35

    Returns: -1

  47. 31

    55

    14

    Returns: 1114

  48. 26

    92

    32

    Returns: 768

  49. 37

    96

    58

    Returns: 1498

  50. 50

    35

    0

    Returns: 2730


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: