Statistics

Problem Statement for "BearPermutations2"

Problem Statement

Bear Limak loves algorithms and tolerates data structures. Today he learned about the Cartesian tree. You can find a detailed description of this tree at https://en.wikipedia.org/wiki/Cartesian_tree. Below we give a shorter description that is sufficient to solve this problem.


Let A be a sequence of distinct numbers. Each such sequence defines a unique Cartesian tree. The Cartesian tree is determined by the following rules:

  1. The Cartesian tree is a rooted binary tree.
  2. The nodes of the tree correspond to the elements of A.
  3. An in-order traversal of the tree prints the sequence A.
  4. The tree is a heap.


A more detailed explanation of the third rule: Consider any node in the tree, and let x be the number in this node. All numbers in the left subtree must appear in A before x, and all numbers in the right subtree must appear in A after x.


A more detailed explanation of the fourth rule: For each node, the number in the node must be strictly smaller than each of the numbers in the children of this node.


Below is a figure that shows the Cartesian tree determined by the sequence A = {9,3,7,1,8,12,10,20,15,18,5}.



We will now define the score of a Cartesian tree. Let T be the Cartesian tree determined by the sequence A. In the tree T there are some nodes that have two children. For each such node, look at the numbers in those two children, find the indices of those two numbers in A, and compute their (positive) difference. The score of the tree T is the sum of all these differences.


For example, in the above tree we have four nodes that have two children each: the nodes with numbers 1, 3, 10, and 15. The children of node 1 are the nodes 3 and 5. In the original sequence A, the values 3 and 5 have (0-based) indices 1 and 10. The difference between these indices is 10 - 1 = 9. For the other three nodes with two children, the differences between their indices are 2, 3, and 2. Hence, the score of this tree is 9 + 2 + 3 + 2 = 16.


You are given the ints N and MOD. There are N! permutations of the numbers 1 through N. Each of these permutations determines a Cartesian tree. Let X be the sum of the scores of these N! trees. Compute and return the value (X modulo MOD).

Definition

Class:
BearPermutations2
Method:
getSum
Parameters:
int, int
Returns:
int
Method signature:
int getSum(int N, int MOD)
(be sure your method is public)

Constraints

  • N will be between 1 and 100, inclusive.
  • MOD will be between 3 and 10^9, inclusive.
  • MOD will be prime.

Examples

  1. 3

    502739849

    Returns: 4

    For N=3 there are 3! = 6 distinct permutations. Four of these produce Cartesian trees with score 0. The remaining two are the permutations (2,1,3) and (3,1,2). Each of these produces a Cartesian tree with score 2. Hence, the sum of all six scores is 0+0+0+0+2+2 = 4.

  2. 1

    1000003

    Returns: 0

  3. 4

    973412327

    Returns: 38

  4. 100

    89

    Returns: 49

  5. 2

    209349097

    Returns: 0

  6. 5

    342896621

    Returns: 324

  7. 6

    564426959

    Returns: 2868

  8. 7

    138715163

    Returns: 27264

  9. 8

    341869421

    Returns: 280656

  10. 13

    921798887

    Returns: 330291877

  11. 21

    7

    Returns: 0

  12. 35

    5566027

    Returns: 1903145

  13. 66

    471984137

    Returns: 16107641

  14. 67

    203675683

    Returns: 132330098

  15. 79

    47448673

    Returns: 30194896

  16. 97

    581697583

    Returns: 174741148

  17. 98

    70588717

    Returns: 36075592

  18. 99

    116662387

    Returns: 67850104

  19. 100

    918872377

    Returns: 515340087

  20. 99

    973412327

    Returns: 305083072

  21. 99

    100000007

    Returns: 12430883

  22. 100

    502739849

    Returns: 215946031

  23. 100

    3

    Returns: 0

  24. 4

    37

    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: