Postupně se vyzkouší všechny možné základy.
Řešení pro jeden základ: dynamické programování.
cnt[i] - počet způsobů, kterými lze rozdělit prvních i znaků
cnt[0] = 1; for ( i = 1; i <= n; ++i ) { cnt[i] = 0; for ( j = 0; j < i; ++j ) if ( valid_digit (j,i) ) cnt[i] += cnt[j]; }
int one_radix (int r) { int i, j; int n; n = numlen; /* radix cannot begin with zero */ if ( number[n-r] == ZERO ) return 0; /* a little piece of dynamic programming */ cnt[0] = 1; for ( i = 1; i <= n - r; ++i ) { cnt[i] = 0; for ( j = (i - r < 0) ? 0 : (i - r); j < i; ++j ) { /* now check whether characters between indices `i' (incl) and `j' (excl) form a valid digit of the given radix */ /* cannot begin with zero unless it is a single character */ if ( j+1 < i && number[j] == ZERO ) continue; /* the first digit cannot be zero if any other follow */ if ( j == 0 && n-r > 1 && number[j] == ZERO ) continue; /* digit must be less than radix */ if ( j + r == i ) if ( strncmp (number + j, number + n-r, r) >= 0 ) continue; /* it is ok, add the number of possible splits */ cnt[i] += cnt[j]; } } return cnt[n-r]; }