TopcoderArchiveVisit Topcoder
SRM6 Mar 2003
SRM 137

PQNumbers

Problem statement, definition, constraints, and public examples.

Problem Statement

For any integers p and q, both greater than one, define the [p,q]-numbers to be all integers of the form (p^i * q^j), for non-negative integers i and j. Your task is to calculate the nth-smallest [p,q]-number, where nth-smallest means that there are exactly n smaller [p,q]-numbers.

For example, the sequence of [2,3]-numbers is 1,2,3,4,6,8,9,12,..., so 12 is the nth-smallest [2,3]-number for n=7.

Create a class PQNumbers containing a method nthSmallest that takes three integers, p, q, and n, and returns the nth-smallest [p,q]-number.

Definition

Class:
PQNumbers
Method:
nthSmallest
Parameters:
int, int, int
Returns:
int
Method signature:
int nthSmallest(int p, int q, int n)
(be sure your method is public)

Notes

  • p^i means p to the i-th power, not xor.

Constraints

  • p and q are both between 2 and 20, inclusive.
  • n is between 0 and 20, inclusive.
  • The nth-smallest [p,q]-number is no greater than 1 million.

Examples

  1. 2
    3
    7
    Returns: 12
    The example above.
  2. 7
    5
    15
    Returns: 3125
  3. 10
    4
    15
    Returns: 4000
  4. 9
    6
    17
    Returns: 17496
  5. 2
    8
    10
    Returns: 1024
  6. 7
    13
    0
    Returns: 1
Back to all problems