TopcoderArchive
Archive
Algorithm problem

ProdCalc

Problem statement, definition, constraints, and public examples.

Problem Statement

You have a calculator that can only handle numbers with up to d digits. Initially, the number shown on the screen is 1. You must perform exactly op multiplication operations. Each operation multiplies the number on the screen by a digit between 2 and 9, inclusive. The result of each operation must be a number with d or less digits. Return the largest number that can be displayed on the screen after op operations. If there is no way to perform op operations without overflowing the calculator, return -1.

Definition

Class:
ProdCalc
Method:
highest
Parameters:
int, int
Returns:
long
Method signature:
long highest(int d, int op)
(be sure your method is public)

Constraints

  • d will be between 2 and 8, inclusive.
  • op will be between 0 and 30, inclusive.

Examples

  1. 4
    3
    Returns: 729
    The calculator can store up to 4 digits. With 3 multiplications, we can obtain at most 729, by using digit 9 every time: 1 * 9 * 9 * 9 = 729.
  2. 8
    0
    Returns: 1
    No multiplications can be performed.
  3. 2
    3
    Returns: 98
    The storage is limited to 2 digits. The highest number that can be shown on screen after 3 multiplications is 98 (2 * 7 * 7).
  4. 8
    10
    Returns: 99574272
  5. 8
    20
    Returns: 99532800
  6. 8
    30
    Returns: -1
    Even if we would only use digit 2 every time, 2^30 has more than 8 digits and overflows the calculator.
  7. 2
    6
    Returns: 96
  8. 3
    1
    Returns: 9
Back to all problems