TopcoderArchive
Archive
Algorithm problem

RearrangeAndMultiple

Problem statement, definition, constraints, and public examples.

Problem Statement

Sometimes it is possible to rearrange the digits of a positive integer to get a different positive integer that is a multiple of the original number. For example, the number x = 142857 does have this property if we use base 10. In base 10 we can rearrange the digits of x to get the number 285714, and we have 285714 = 2 * 142857.

(Note that adding leading zeros to the original number is not allowed. For example, it is not allowed to write the number 7 as 007 and then to rearrange those digits to get 700.)

You are given the ints L and b. Your task is to construct a positive integer x with the following properties:
  • When written in base b, the integer x has exactly L digits.
  • We can rearrange the base-b digits of x to get a different L-digit number that is a multiple of x.
If there is no such number, return an empty String. Otherwise, return a String that encodes the integer x as explained below. If there are multiple solutions, you may return any of them.

In order to return a positive integer x, do the following:
  1. Take each of its base-b digits separately.
  2. Write the value of that digit in base 10.
  3. Return a String that contains all those values separated by single spaces.
For example, if b is 10 and x is 142857, you should return "1 4 2 8 5 7". Another example: if b is 16 and x is 2A0F (when written in base 16), you should return "2 10 0 15".

Definition

Class:
RearrangeAndMultiple
Method:
construct
Parameters:
int, int
Returns:
String
Method signature:
String construct(int b, int L)
(be sure your method is public)

Constraints

  • b will be between 2 and 100,000, inclusive.
  • L will be between 1 and 100, inclusive.

Examples

  1. 10
    6
    Returns: "1 4 2 8 5 7"
    We can rearrange 142857 into 284714 which is 2 * 142857. Note that we can also rearrange it into 428571 = 3 * 142857 as well.
  2. 10
    4
    Returns: "1 0 3 5"
    3105 = 1035 * 3
  3. 10
    14
    Returns: "1 4 2 8 5 7 1 0 3 5 1 0 3 5"
    42857131053105 = 3 * 14285710351035
  4. 16
    5
    Returns: "7 11 13 14 15"
    F7BDE = 2 * 7BDEF.
  5. 1234
    2
    Returns: "246 987"
  6. 2
    97
    Returns: ""
Back to all problems