UVa 10093 - An Easy Problem!

10093 - An Easy Problem!

 * http://acm.uva.es/p/v100/10093.html

Summary
Given any based number you have to find the minimum base for which that number will be divisible by N-1 in N base.

Explanation
If this problem is not as easy as its name suggests, then it's not as hard as you may think. You have to find the minimum valid base for that number (maximum digit + 1) and try to divide that number in that base by base-1 to check if divisible until you reach base 62. If it's not divisible by this time then the number is impossible.

The number can be very big to be held in any datatype. So try to divide digit by digit. This problem can be much easier if you know the following formula. Thanks to P.A.Petrov.

An N-based number R is divisible by (N-1) if and only if the sum of its digits is divisible by (N-1).

Gotcha's
No base can be less than 2. There can be negative numbers. Just ignore the sign.

Input
123456789ABCDEFGHIJKLMN

Output
24