TopCoder problem "BasesPerm" used in SRM 16 (Division I Level Two , Division II Level Two)

Problem Statement

Class name: BasesPerm
Method name: getNthSame
Parameters: int, int, int
Returns: int

Implement a class BasesPerm, which contains a method getNthSame. The method
takes three ints as parameters:  an index n and two bases.  getNthSame returns
the nth non-negative base 10 integer which can be represented in two specified
bases by permutations of the same set of digits.

Example: the 13th base 10 integer that can be represented by permutations of
the same set of digits in base 5 and 7 is 2576.
2576 base 10 == 40301 base 5 == 10340 base 7

As usual, bases that have a radix > 10 use the alphabet following '9'.
i.e. 0 1 2 3 4 5 6 7 8 9 A B C D E F...

Note however, that 687 (687 base 10 == 4212 base 5 == 1424 base 7) is not an
integer that can be specified by permutations of the same set of digits in base
5 and 7 because there are two 2's in the base 5 representation and one 2 in the
base 7 representation and are therefore not permutations of each other.  Also,
leading 0s cannot count: 0542 base b1 and 5402 base b2 are not valid
permutations of each other.

If there are not at least the index number of numbers that can be represented
in both bases as permutations of each other, the method returns -1. For
instance, with parameters of 20, 2, 10, the first two numbers are 0 and 1, but
beyond that, there exist no numbers in base 2 and 10 that are permutations of
each other so the method returns -1.

The method signature is:
public int getNthSame(int nth, int base1, int base2);

Input restrictions:
0 < nth < 21
1 < base1 < 16
1 < base2 < 16

*n=1, base1=4, base2=8 returns 0.
*n=4, base1=8, base2=16 returns 3.
*n=9, base1=8, base2=16 returns -1.
*n=10, base1=9, base2=16 returns 4213.
*n=5, base1=2, base2=2 returns 4.


Parameters:int, int, int
Method signature:int getNthSame(int param0, int param1, int param2)
(be sure your method is public)

Problem url:

Problem stats url:




Problem categories: