Statistics

Problem Statement for "GradedOrder"

Problem Statement

A monomial is a product of non-negative powers of variables, with an implied coefficient of 1. For example, x, xy, x2y, xyz, and 1 are all monomials.

A monomial formed from a set of n variables (specified in some particular order) can be represented as a vector of non-negative integers of length n. The ith element of this vector represents the power of the ith variable in our set of variables.

For example, let our set of variables be { x, y, z }. Then we have the following vectors for the following monomial expressions:

  • x = [ 1, 0, 0 ]
  • xy = [ 1, 1, 0 ]
  • x2y = [ 2, 1, 0 ]
  • xyz = [ 1, 1, 1 ]
  • 1 = [ 0, 0, 0 ]

The degree of a monomial is then the sum of the elements of its vector representation. For instance, x has degree 1 and x2y has degree 3.

Now we can define an ordering on monomials, based on their vector representations. First we will define what is known as reverse lexicographic order. This ordering states that a monomial v comes before a monomial w if the left-most non-zero element of v - w is positive. For example, xyz comes before xy under a reverse lexicographic ordering. In this case, v - w is given by

[ 1, 1, 1 ] - [ 1, 1, 0 ] = [ 0, 0, 1 ]

The first non-zero element of v - w is 1, which is positive, so xyz comes before xy.

Now we will extend this ordering to define a more complex one, which is known as graded reverse lexicographic order (or simply graded order). Under the graded order, a monomial v comes before a monomial w if the degree of v is greater than the degree of w, or if both have the same degree and v comes before w under the reverse lexicographic order. For instance, the five monomials in the example above, sorted by graded ordering, are:

x2y, xyz, xy, x, 1

The monomials x2y and xyz have the same degree, but x2y comes before xyz under the reverse lexicographic ordering. All of the other monomials have different degree, and are shown in descending order of degree.

A polynomial of degree n can be formed from a list of variables by combining all of the monomials of degree less than or equal to n that can be formed from these variables. We will ignore constant coefficients in this problem, except for the monomial of degree 0, which we will denote as 1. We will call such a polynomial a "full polynomial." For example, if n = 3 and the list of variables is [ x, y ], then the full polynomial, with its terms listed in graded order, is:

x3 + x2y + xy2 + y3 + x2 + xy + y2 + x + y + 1

In this full polynomial, x3 is the 0th term, x2y is the 1st term, and 1 is the 9th term. Note that the number of terms in a full polynomial of degree n formed from m variables is given by:

C(n + m, m) = (n + m)! / (n! m!)

For n = 3 and m = 2, we have C(5, 2) = 5! / (3! 2!) = 10.

Given a list of variables, an integer n, and an index k, you are to return the String representation of the kth term in the full polynomial formed by these parameters, with its monomial terms sorted in graded order. The terms must be sorted by graded order before their positions are determined. The list of variables will be given as the String vars, where the ith character specifies the ith variable. The String representation of the monomial that should be returned is as follows.

  1. If the degree of the monomial is 0, its representation is "1"
  2. Otherwise, its representation is of the form "<var><var>...<var>", where <var> is the String representation of a variable and its power in the monomial. The vars must appear in the order that they appear in the vars parameter to getTerm, and are of the following form:
    1. If the power of the variable in the monomial is 0, it should be represented by the empty string (that is, not appear at all)
    2. If the power of the variable in the monomial is 1, it should be represented by only its name (e.g., "x")
    3. Otherwise, the variable should be represented as its name, followed by the '^' character, followed by its power, represented as a decimal integer with no leading zeros. For example, "x^2", "y^3", etc.

Definition

Class:
GradedOrder
Method:
getTerm
Parameters:
String, int, int
Returns:
String
Method signature:
String getTerm(String vars, int n, int k)
(be sure your method is public)

Notes

  • vars is a String specifying the names and order of the variables (each variable is a single character)
  • n is the degree of the full polynomial to be formed
  • k is the position of the term (in graded order) to be returned (counting from 0)

Constraints

  • vars will have length between 1 and 26 inclusive
  • vars will consist only of lowercase letters ('a'-'z'), and no letter will appear more than once
  • n will be between 0 and 10 inclusive
  • k will be between 0 and (n + m)!/(n! m!) - 1 inclusive, where x! denotes x * (x-1) * (x-2) * ... * 2 * 1 and m is the length of vars

Examples

  1. "xy"

    3

    2

    Returns: "xy^2"

    This example corresponds to the full polynomial shown above: x3 + x2y + xy2 + y3 + x2 + xy + y2 + x + y + 1 The monomial in position 2 is xy2, so the method should return "xy^2"

  2. "xy"

    3

    0

    Returns: "x^3"

  3. "xy"

    3

    9

    Returns: "1"

  4. "x"

    6

    4

    Returns: "x^2"

    The number of terms in the full polynomial is C(7, 1) = 7! / (6! 1!) = 7. Generating all the possible monomials and sorting them yields: x6 + x5 + x4 + x3 + x2 + x + 1 The term in position 4 is x2, so the method should return "x^2"

  5. "abcdefghijklmn"

    10

    1

    Returns: "a^9b"

  6. "abcdefghijklmn"

    10

    1000000

    Returns: "d^2gk^2n^5"

  7. "abcdefghijklmnopqrstuvwxyz"

    10

    0

    Returns: "a^10"

  8. "x"

    1

    1

    Returns: "1"

  9. "abcdefghijklmn"

    0

    0

    Returns: "1"

  10. "x"

    10

    0

    Returns: "x^10"

  11. "x"

    10

    6

    Returns: "x^4"

  12. "x"

    10

    10

    Returns: "1"

  13. "abcdefghijklmn"

    10

    965769

    Returns: "d^4g^2himn"

  14. "abcdefghijklmn"

    10

    450000

    Returns: "ae^4fl^2n^2"

  15. "xyz"

    3

    0

    Returns: "x^3"

  16. "xyz"

    3

    10

    Returns: "x^2"

  17. "wxyz"

    7

    37

    Returns: "w^2x^4z"

  18. "nopqrstuvwxyz"

    10

    1000000

    Returns: "o^2qtuwxy"

  19. "abcdefghijklmnopqrstuvwxyz"

    10

    10000000

    Returns: "a^2efhjn^2ov"

  20. "abcdefghijklmnopqrstuvwx"

    10

    12167769

    Returns: "abent^2u^2x^2"

  21. "abcdefghijklmnopqrstuvwxyz"

    10

    12062468

    Returns: "a^2ginq^2svw"

  22. "abcdefghijklmnopqrstuvwxyz"

    10

    254000000

    Returns: "n^2t^2uw"

  23. "abcdefghijklmnopqrstuvwxyz"

    8

    0

    Returns: "a^8"

  24. "abcdefghijklmnopqrstuvwxyz"

    10

    48574329

    Returns: "ahip^2t^4u"

  25. "a"

    0

    0

    Returns: "1"

  26. "abcdefghij"

    10

    33098

    Returns: "abcdefghij"

  27. "abcdefghij"

    10

    48620

    Returns: "b^10"


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: