We need to parse input string "a:b". For each possible base? But there's infinite possibilities if base can be arbitrarily large. However constraints: a and b are strings length up to 5, and characters from '0'-'9' and 'A'-'Z' representing digits 0-35. So the maximum digit value is at most 35. In any base, the digits used must be less than the base. So for base > max_digit, digits are valid. But also the numeric value of the string (interpreted in that base) must satisfy: hours: 0 <= H < 24, minutes: 0 <= M < 60. For large enough base, the value will become large (since digits are interpreted as base^position). So only finitely many bases can produce small numbers. In fact, as base increases, the value of a fixed-length string with leading zeros? Actually string has fixed digits. For example, "000B" has length 4. Its value in base B is B (since leading zeros are zeros). As base increases, the value increases linearly with base? For a string of digits d_{n-1}...d0, its value = sum d_i * base^i. So it's a polynomial in base with degree len-1. As base grows, the value grows to infinity. So eventually hours >=24 or minutes >=60, unless the string is "0" or something that can be zero? But if the string consists entirely of zeros, then value is always 0 regardless of base. In that case, if both a and b are all zeros? Actually a could be "0" or "000" etc. So if a is all zeros, then its value is 0 for any base (provided base>1). Similarly for b. So if both a and b are all zeros, then for any base > max_digit? Actually if string is all zeros, then value 0 for any base. So infinite many bases (any base >= 2 that makes digits valid: but if it's all zeros, digits are zero, so any base >= 2 works). But careful: If a is all zeros, value is 0. Then hours=0 which is valid (0 <= 0 <24). Similarly b all zeros gives minutes=0 valid. So if both are all zeros, then infinite number of bases (any base >=2). However, there is also the condition that digits must be less than base. With all zeros, that's always true for any base >=2 (since 0<base). So infinite. But note: base must be > max digit value, but max digit value is 0, so base must be >0? Actually "positive radix strictly larger than one". So base must be >=2. So indeed infinite.
If only one of them is all zeros and the other is something else, we still have finite possibilities? For the non-zero string, as base increases, its value increases, so eventually it will exceed bound. So there will be finite number of bases. However, could there be infinite if the non-zero string also yields constant value? That can only happen if the string represents the number zero (all zeros) or if it's something like "0" but with letters? Actually any string containing a non-zero digit at any position will produce value that increases with base (unless the leading digit? Actually if the highest non-zero digit is at position i, then the term d_i * base^i dominates, so value grows unbounded as base grows). So for a non-zero string, the number of bases for which the value is within the given ranges (0-23 for hours, 0-59 for minutes) is finite. But we also need the digits to be valid for the base (each digit < base). So there is a lower bound: base > max_digit. So there is a minimum base: base_min = max(max_digit in a or b) + 1. Actually for each string, the base must be > each digit value, i.e., base > max_digit across both a and b. So base_min = (max_digit + 1). But also base must be at least 2. So base_min = max(2, max_digit+1). So we only consider bases >= base_min.
Now we need to find all bases (integer >= base_min) such that:
- value(a, base) < 24
- value(b, base) < 60
If there are infinitely many such bases, output -1.
If none, output 0.
Otherwise, list them in increasing order.
So the main challenge: determine if infinite many bases exist. As argued, infinite many bases occur if and only if both a and b represent the number 0 for all bases (i.e., the strings consist only of zeros). But wait: Could there be a scenario where value(a,base) is constant for all bases? That would require that the polynomial sum d_i * base^i is constant. That only occurs if all digits are zero except possibly constant term? Actually if we have string like "0" (length 1) it's zero. But if string is something like "00", it's still zero. So indeed only if the numeric value is zero independent of base, i.e., all digits are zero. Similarly for b. So infinite if a is all zeros and b is all zeros. But could there be infinite if the constraints on hours and minutes allow arbitrarily large bases? That would require that for all sufficiently large bases, the values remain within [0,23] and [0,59] respectively. But as base increases, values increase unless they are identically zero. So they cannot stay bounded. So infinite only when both values are identically zero.
But wait: Consider a = "0", b = "0"? That yields infinite. What about a = "0", b = "00"? Same. Yes.
But what about a = "0", b = "1"? For base large, value of b becomes 1 (since "1" is digit 1 times base^0? Actually "1" length 1, so value = 1, constant regardless of base? Wait: If the string is "1", it's a single digit 1, so value = 1 for any base >=2? Actually, in base b, digit '1' means value = 1. So if the string has length 1 and digit d, then value = d, constant for any base >= d+1? But careful: For a single-digit string, the value is just the digit, independent of base, as long as base > digit (so digit is valid). For example, "A" (10) yields value 10 for any base >10. So "1" yields value 1 for any base >=2 (since digit 1 < base). So if a is "0" and b is "1", then for any base >=2, hours=0, minutes=1, which is a valid time (0:01). So that also gives infinite bases? Let's test: b = "1" single digit 1. For base 2: value 1 (since 1 < 2), yes. For base 100: value 1. So minutes always 1. So it's always <60. Hours = 0. So time valid for all bases >= max_digit+1? max_digit in a? a = "0", digit 0; b = "1", digit 1; max_digit=1 -> base_min = 2. So for all bases >=2, the representation is valid. That yields infinitely many bases! Similarly, a = "2", b = "0"? a is single digit 2, value = 2 for any base >2? Actually base must be >2 for digit 2 to be valid. So for base >=3, hours=2, minutes=0, valid (2:00). So infinite.
But careful: If a is a single-digit value less than 24, and b is a single-digit value less than 60, and both are constant across bases, then any base >= (max_digit+1) will work, giving infinite many. More generally, if the value of the string does not depend on base, i.e., the string length is 1, the value is just the digit, constant. Also if string length > 1 but contains only zeros? That is constant zero. Or perhaps strings like "10"? That's length 2; its value in base b is 1b + 0 = b, which grows with b. So it's not constant. So only strings with length 1 (or all zeros) give constant value. However, what about strings like "0X"? "0X" where X is a digit? That's still zero in the most significant digit? Actually if the most significant digit is zero and the next is non-zero, the value is d0 * base^0? No, we have to consider positional notation: e.g., "01": this has length 2, digits: d1 (most significant) = 0, d0 = 1, so value = 0base + 1 = 1, constant? Wait, "01" length 2, but the most significant digit is '0', so effectively the string may have leading zeros. If the string has leading zeros, the numeric value can be independent of base if the only non-zero digits are in the least significant positions? Let's analyze: For any string, if all digits except possibly the least significant digit are zero, then the value is just the least significant digit, constant regardless of base. For example, "0005" (length 4) has digits: d3=0,d2=0,d1=0,d0=5 -> value = 5, constant. So indeed any string that, after ignoring leading zeros, consists of a single digit (i.e., the number is less than the base, but more importantly, its representation can have arbitrary number of leading zeros) yields constant value across bases, as long as the base is large enough that the digits are valid. Because the polynomial reduces to just d0, since higher positions are zero. So the value is independent of base. So condition for infinite bases is: both a and b, when interpreted as numbers, have values that are constant with respect to base, i.e., the numeric value does not depend on base (i.e., they are essentially single-digit numbers after ignoring leading zeros). But careful: If the string has a non-zero digit in any position other than the least significant, then the value depends on base. For example, "10" has a digit 1 in tens place? Actually it's base-dependent. So constant value occurs exactly when the number is represented by digits where all but the last digit (units) are zero. That is, the string's non-zero part is only at the least significant digit. Equivalently, the numeric value is equal to the integer represented by the last character (or zero if all zeros). And this value is constant for any base >= (max_digit+1) because leading zeros contribute nothing.
But there is also the case where the numeric value could be zero regardless of base if all digits are zero. That's also constant.
So infinite many bases if and only if both a's value is constant (independent of base) and b's value is constant, and both satisfy the bounds (hours <24, minutes <60) for large bases, which they will because the constant values are fixed. However, we must also check that for bases arbitrarily large, the digit validity holds: digits must be less than base. Since max_digit is fixed, for any base > max_digit, digits are valid. So indeed for all bases > max_digit, the interpretation yields the same constant numbers (since higher digits are zero). So if those constant numbers are within the ranges, then there are infinitely many bases (all integers >= base_min). That yields infinite.
But wait: Could there be a scenario where a's value is constant but b's value is not, yet for infinitely many bases the b's value also stays within bound? Since b's value grows with base, it will eventually exceed bound, so only finitely many. So infinite only if both are constant (i.e., both strings have no non-zero digit except possibly the least significant digit). But also we need to consider the possibility that a's value is constant and b's value is also constant? Actually both constant.
Thus algorithm: Parse input into two strings a_str and b_str. For each, compute:
- maximum digit value max_digit (treat '0'-'9' as 0-9, 'A'-'Z' as 10-35).
- Determine if the string represents a constant-value number across bases: i.e., after stripping leading zeros? Actually we can compute its value in any base if it's constant. But easier: The value is constant iff all characters except possibly the last (rightmost) are '0'. Because if any character before the last is non-zero, then the value depends on base (since its weight is base^i for i>=1). But wait, what about a string like "0A0"? That's digits: d2=0, d1=10, d0=0. The value = 10 * base^1 + 0 = 10*base, which depends on base. So not constant. So condition: For the string, all characters except possibly the last one must be '0'. Or equivalently, the integer value is equal to the digit value of the last character. But if the last character is also zero? That means the whole string is zeros (all characters '0'), which also yields constant zero. So condition: For i from 0 to len-2 (positions from left except the last), digit at i must be 0. The last position can be any digit. That ensures value = digit_last, constant.
Check: "00A": length 3, digits: d2=0, d1=0, d0=10 -> constant 10. Indeed all except last are zero. So yes.
Thus we can test for each string:
is_const = True
for i in range(len(s)-1):
if digit(s[i]) != 0: is_const = False; break
If is_const, then value = digit(s[-1]) (if s is all zeros, then digit_last is 0 as well). That value is independent of base.
Now infinite condition: both a and b are constant (is_const_a and is_const_b) and their values are <24 for hours and <60 for minutes? Actually hours must be <24 (since 0-23) and minutes <60. But if they are constant and satisfy these bounds, then for any base >= base_min, the time is valid. That yields infinitely many bases. However, we must also ensure that base_min itself maybe not satisfy the bounds? But base_min is defined as max_digit+1, which is always >=2. For any base >= base_min, digits are valid. And values are constant and within bounds. So infinite.
But need to also consider the case where a is constant but value >=24? Then no base can work because value would be >=24 for all bases >= base_min? But could a base smaller than max_digit+1 be considered? No, because digits must be less than base. So if a is constant but value >=24, then there is no base that can make hours valid, because value is fixed >=24. So output 0. However, infinite condition only if both constant and within bounds.
Now if not infinite, we need to find all bases (integer) >= base_min such that hours < 24 and minutes < 60.
Since the strings are of length <=5, the values grow as polynomial. The maximum possible base we need to check? Since hours must be <24, we can find the maximum base for which hours could be <24. The smallest possible value for a given length is when digits are minimal? Actually we can iterate over possible bases from base_min upwards until the value of a (or b) exceeds the bound. But there might be many bases? Hours bound 24 is small, so number of bases that give hours <24 is limited. But careful: a may have leading zeros and length up to 5, but its value is sum d_i * base^i. For base just above max_digit, it could already be >24. For base large, it increases quickly. So we can loop base from base_min to some upper bound where value_a >= 24 or value_b >= 60. But we need to consider both: we need bases where both are within bounds. The upper bound for base might be determined by when either a >= 24 or b >= 60. However, what if a stays less than 24 for many bases? For a constant string (like "1") it's always 1, so it's always <24 regardless of base. But if it's constant, we already handled infinite case. So for non-constant strings, the value increases with base, so eventually it will exceed bound. So we can find the maximum base such that value_a < 24. Since base is integer, we can find it by solving polynomial inequality. But simpler: loop base from base_min up to some max, but we need to know when to stop. Since bounds are small (24 and 60), and length <=5, the maximum base that could possibly keep a <24 might be not too high. For a string like "1" (constant) we already said infinite, so not in this branch. For a non-constant string, the smallest non-constant might be something like "10" (digits: 1 and 0). In base b, value = 1*b + 0 = b. So b must be <24, so base <24. So base_max for a could be at most 23 if leading digit >0. But consider "01"? That is constant 1 because leading zero, so constant, handled infinite. So for non-constant a, the highest digit at the most significant position (the leftmost) must be non-zero; else it would be constant because all higher are zero? Actually if the leftmost digit is non-zero and length L, then the value is at least that digit * base^(L-1). So to keep <24, base cannot be too high. For L=5 and digit=1, base^(4) <= 23 -> base <= floor(23^(1/4)) ~ 2, so base_max very small. So indeed the maximum base that can keep a <24 is limited. Similarly for b <60, b can be a bit larger. But overall, we can just loop base from base_min up to some reasonable upper bound like 100? But need to ensure we cover all possible bases that might satisfy both. Could there be a case where a is non-constant but still <24 for bases up to, say, 100? Let's test: a = "1" (constant) not considered. a = "2" constant. a = "01" constant. a = "001" constant. So non-constant a means there is at least one non-zero digit in a position i where i>=1 (i.e., not the last). So the value is at least digit_i * base^i, with i>=1. For base >=2, digit_i >=1, so value >= base. So to have value <24, we need base <24. Actually because the smallest non-zero digit at position >=1 is at least 1, so value >= base^(1) maybe? Wait: If the highest non-zero digit is at position k (k>=1). Then value >= digit_k * base^k. Even if digit_k=1 and k=1, value >= base. So indeed base must be <24. So max base for a to be <24 is at most 23. Similarly, for b, if it's non-constant, value >= base (since at least one non-zero digit at position >=1), so base must be <60. But if b is constant, we might have infinite? But then both constant case is infinite, already handled. So in the non-infinite case, at least one of a or b is non-constant. If a is non-constant, base <= 23; if b is non-constant, base <= 59. So overall base is bounded by max of these: if both non-constant, base <= min(23,59)=23. But if a constant and b non-constant, then base is bounded by b's condition: b non-constant => base <=59. So base could be up to 59. But wait, constant a could be like "23"? That's <24. And b non-constant could be "10" (value=base). For base=50, hours=23 (constant) valid, minutes=50 (<60) valid. So base could be as high as 59? Let's check: b non-constant, value >= base (since at least one non-zero digit in a higher position). Actually if b is constant we are in infinite case only if both constant. So if a constant and b non-constant, b could be something like "0Z"? "0Z" has digits: d1=0, d0=35. That's constant? Actually "0Z": the first digit (position 1) is 0, last digit is 35. So it is constant (since only last digit non-zero). That would be constant, so b constant. So for b to be non-constant, it must have a non-zero digit in some position other than the last. So its minimal value is at least digit_i * base^i, with i>=1. The smallest possible non-zero digit_i is 1, and i could be 1 if the digit at tens place is 1? But if the highest non-zero digit is at position 1 (the second from right), then value >= base. However, there could be a scenario where the non-zero digit is at position 1 but the lower digits could be negative? No. So minimal value for b is at least base (if digit=1 and i=1). But what about b = "10": value=base. So yes, b >= base. So base <= 59. So upper bound 59.
If a constant and b non-constant, base could be up to 59. But what about base > 59? Then b would be >= base >=60, so invalid. So we only need to check bases from base_min up to 60? Actually up to maybe 60 inclusive? For base=60, b could be exactly 60? If b non-constant, minimal value is base; so base=60 gives b>=60, but minutes must be <60, so b=60 invalid. So base <60. So base_max <= 59. So the maximum base we need to consider is 60? Actually if b non-constant, base <=59. If a non-constant, base <=23. So we can safely set an upper bound of 60 (or maybe 100 to be safe). But careful: Could there be a case where b is non-constant but its value could be less than base? For example, if the highest non-zero digit is at position 1 but digit=1 and the lower digits are negative? Not possible. So indeed value >= base^1 * 1 = base. So b >= base. So to have b <60, base <60. So maximum base to check is 59.
If both non-constant, base <=23.
Now, also need to consider base_min might be larger than these bounds, then no bases.
Thus we can iterate base from base_min to 60 (or maybe up to 100 to be safe) and check conditions. Since string lengths up to 5, evaluating polynomial for each base is cheap.
But there's a nuance: For constant strings (like a constant) we are not in infinite case only if the other string is non-constant. In that scenario, we still need to consider bases from base_min up to some bound. But the constant value may be > bound? For example, a constant but value 30, which is >=24, then no base works. But we would detect that when checking base_min: value_a constant 30 >=24, so none. So we can still loop: but if constant a value >=24, then for any base >= base_min, hours invalid, so no bases. So we could early exit.
But the loop from base_min to 60 will capture all possible valid bases, because for constant a, the value is constant, and we check if it's <24. If not, none.
Thus algorithm:
- Read line, split by ':' to get a_str and b_str.
- Compute for each string:
- digits list: convert each char to integer value (0-35).
- max_digit: maximum digit value.
- is_const: check if all digits except the last are zero.
- if is_const: const_val = last_digit value.
- Determine overall max_digit = max(max_digit_a, max_digit_b)
- base_min = max(2, max_digit+1) // because base must be > each digit value
- If is_const_a and is_const_b:
if const_val_a < 24 and const_val_b < 60:
// infinite many bases
// However, must also ensure that base_min itself is defined? For infinite, base can be any >= base_min.
// But is there any possibility that base_min is so high that it's not valid? Actually base_min is just the smallest base for which digits are valid. For any base >= base_min, digits are valid. Since values are constant and within bounds, all such bases are valid. So output -1.
// But we should check that const_val_a and const_val_b satisfy the bounds. If they do, output -1.
// else output 0 (since no base works, because constant values are out of range).
// But also need to consider that base_min might be > some threshold? No, base_min does not affect validity if values are constant and within bounds.
return -1 or 0 accordingly.
- Else (at least one non-constant):
Initialize result list.
// We need to consider bases from base_min up to some upper bound.
// We need to consider all bases where value_a < 24 and value_b < 60.
// Since at least one string is non-constant, the valid bases are finite. We can iterate base from base_min up to, say, 100 (or more precise up to 60 or maybe 1000? but safe bound could be something like 10^6? But we can compute a maximum base beyond which one of the values will exceed bound.)
// Let's derive an upper bound.
// For a string that is non-constant, its value is at least base^(len-1) if the leading digit is >=1? Actually minimum non-zero leading digit is 1. So value >= base^(len-1). To be <24 (for a) or <60 (for b), we need base^(len-1) < bound => base < bound^(1/(len-1)). For len up to 5, len-1 up to 4. For a, bound 23, for b bound 59. So maximum possible base for a is floor(23^(1/4)) = floor( ~2.11) =2, for b floor(59^(1/4)) ~ sqrt(sqrt(59)) ~ 2.77? Actually 59^(1/4) ≈ 2.77, floor=2. That seems too low. But consider a = "10": len=2, len-1=1 => base < 24 because base^1 <24 => base <=23. So bound depends on which position the first non-zero digit is. If the first non-zero digit is at position i (0-index from right? Actually the highest power is len-1. But if there are leading zeros, the highest non-zero digit could be at a lower power. For example, a = "01" is constant, not non-constant. For non-constant, we guarantee there is some digit at index >=1 that is non-zero, but not necessarily the highest index. For a = "0010": digits: d3=0,d2=0,d1=1,d0=0 => value = 1base^1 = base. So base must be <24. So indeed the smallest non-zero digit among positions >=1 gives a lower bound on value: if there is any digit >0 at position i (i>=1), then value >= base^i. The worst case for allowing larger base is when the smallest such i is 1, and the digit is 1. That yields value >= base. So base <= bound-1? Actually value = base + maybe other contributions. So base must be <= bound-1? But could there be a scenario where value < bound for base higher than bound? Suppose a = "0A0"? digits: d2=0,d1=10,d0=0 => value = 10base. For base=2, value=20 <24; base=3 =>30 >=24. So base max is 2. So indeed base cannot exceed bound-1 if the coefficient at base^1 is 1? Actually if coefficient is 1, value = base + maybe lower digits. So to have value <24, base <=23 (since base+ something >= base). So base <=23. If coefficient >1, then base must be lower. So the absolute upper bound is bound-1 (23 for hours, 59 for minutes) for any non-constant string, because value >= base (since at least one digit at i>=1, and its digit >=1). So base_max <= 23 for a non-constant a, and <=59 for a non-constant b. But what about a constant a and non-constant b? Then b's non-constant restricts base <=59, as said. So overall, base_max <= 59. So we can set upper bound = 60 (or 100) safely. But there is one more subtlety: a constant a but value >=24? Already no bases.
But what about a non-constant b and a constant a where constant a value may be >=24? Then no bases. So we can just loop from base_min to 60 (or maybe 100 to be safe) and break when value_a >=24 or value_b >=60? But if a is non-constant, it will eventually exceed 24, but maybe for some base >60? Actually if a is non-constant, base <=23. So base >23, a >=24, so no need to go beyond 23. So we can limit the loop to max( base_min, ... ) up to maybe 100, but it's small.
Given the small bounds, we can simply loop from base_min to 100 (or maybe 200) and break when both a and b are guaranteed to exceed bounds for larger bases? But we need to list all valid bases; if we set a high upper bound like 10^6, it's inefficient? But base_min could be up to 36 (max digit 35 => base_min=36). So from 36 to maybe 60 is at most 25 numbers. That's fine. Even if base_min is 2 and we loop up to 60, that's 59 numbers. So we can just loop from base_min to 60 (or maybe 100 to be safe, but 60 is enough because any base >=60 will cause b (if non-constant) to be at least base >=60 => invalid. If b is constant and a is non-constant, then base >=24 would make a >=24 invalid. If both constant, we already handled infinite. So the only case where both are constant is infinite. If one constant and the other non-constant, base is bounded by non-constant's bound (23 or 59). So indeed max base to consider is 59. However, consider the case where a is constant but b is constant as well? That's infinite. So indeed the only finite cases involve at least one non-constant, and base_max <= 59. So we can set upper bound = 60 (exclusive). But careful: What about a constant and b non-constant but b's minimal value might be > base? Actually b >= base. So base must be <=59. So yes.
But could there be a scenario where b is non-constant but value could be less than base? No, because at least one digit at i>=1, which contributes digit_i * base^i. Since digit_i >=1 and i>=1, that term alone is >= base^i >= base (since base >=2 and i>=1, base^i >= base). So yes.
Thus we can safely set maximum base to 60 (exclusive) i.e., up to 59. But what about base = 60? Could b = 60 be allowed? minutes must be <60, so 60 is not allowed. So base 60 not allowed anyway.
But is there any possibility that base could be larger than 59 and still have b<60? Only if b is constant. But then both constant? Actually if b constant and a non-constant, then base must satisfy a<24, but a non-constant => base <24. So base can't be >59 anyway. If both constant, infinite.
Thus the maximum base to check is 59.
But wait, there is also possibility that a is constant but b is non-constant, and a constant value might be <24, b's value might be <60 for some base >59? Could b's value be less than 60 for base 100 if b's representation is something like "0A"? That's constant 10? Actually "0A" has first digit 0, last digit 10 => constant 10, so b constant. So non-constant b means there is some non-zero digit at a higher position. For b to be non-constant, the highest non-zero digit must be at position i>=1. Then value >= base^i * digit_i. For base >=60, even i=1 and digit_i=1 gives value >=60. So b >=60. So invalid. So base cannot be >=60.
Thus loop base from base_min to 60? Actually inclusive up to 59. We'll include base=59.
But what if base_min is >59? For instance, max_digit=59? But max digit is at most 35 because characters up to Z=35. So base_min = max(2, 36) = 36. So base_min is at most 36. Since 36 <= 59, that's fine. So we loop base from base_min to 60? Actually 60 is not needed. So we can loop base = base_min to 100 (safe) and break if base > 60? But we must check each base up to maybe 60, because a constant a might be valid for base=60? But if a constant and b non-constant, base=60 invalid for b. So no need to check >59. So we can set upper bound = 60.
But I think it's simpler: loop base from base_min while base <= 60 (or while true) but stop when both a and b are guaranteed to be out of range? But we can just loop from base_min to 60 (inclusive) and check condition, but since 60 may produce b=60 which is not allowed, we can still check it and reject. But we might need to consider base=60 if b constant? b constant would be infinite case, already handled. So no. So we can loop up to 60.
But I'll loop up to 100 to be safe, but 100 is fine as it's small.
However, there is also possibility that a and b are both non-constant but base_min is say 36 and base up to 23? That yields no bases. So loop will not run.
Thus algorithm:
-
Parse input.
-
Compute digits arrays for a and b.
-
Compute max_digit.
-
Compute is_const_a, const_val_a; is_const_b, const_val_b.
-
If is_const_a and is_const_b:
if const_val_a < 24 and const_val_b < 60:
print(-1)
else:
print(0)
return.
-
Determine base_min = max(2, max_digit+1).
-
Initialize empty list results.
-
For base in range(base_min, SOME_UPPER):
compute value_a = evaluate(a_digits, base)
if value_a >= 24: break? But careful: if a is constant, value_a won't increase, so break condition not monotonic? For constant a, it's constant. If a is non-constant, it increases with base, so break once it's >=24. But if a is constant and value_a >=24, then for all bases >= base_min, invalid, so we can break after first? Actually if constant a >=24, no base works, so we can output 0 early. So we can check before loop: if is_const_a and const_val_a >=24: print 0 and return. Similarly if is_const_b and const_val_b >=60: print 0 and return. So we can early reject.
-
Now, after those early checks, we know that if a is constant, then its value is <24; if b constant, its value <60. So we only need to loop over bases where the non-constant ones satisfy bounds.
But we need to decide an upper bound for the loop. Since at least one is non-constant, we can bound base by the smallest bound from the non-constant strings. For each non-constant string, the value increases with base, and we can compute for each base. We can simply loop while base <= 60? But careful: If a is constant and b is non-constant, b may become >=60 at some base, and after that all larger bases will also give b >=60 because b is increasing. So we can break when b >=60. But b could be decreasing? No, for a non-constant string, value is increasing with base (since each digit coefficient non-negative and at least one positive coefficient with exponent >=1, the function is strictly increasing for base >= something? Actually for base > 0, polynomial with positive leading coefficient is increasing for base > some threshold? But since coefficients are non-negative and at least one positive coefficient with exponent >=1, the function is strictly increasing for integer base > 0. Because derivative? But we can rely on monotonic increase as base increases because each term base^i is increasing. So once value exceeds bound, it will stay exceeded for larger base. So we can break.
Similarly, if a is non-constant, once a >=24 break.
Thus we can loop base from base_min upwards, while base <= some high limit (say 1000) but we can break as soon as either (a non-constant and value_a >=24) or (b non-constant and value_b >=60). Since we know these values are increasing, after they exceed, no larger base can be valid.
But is it guaranteed that for non-constant strings, the value is strictly increasing? Yes, because if the string has at least one digit with exponent >=1 and digit>0, then as base increases, that term increases, and all terms are non-negative, so the total value strictly increases (since base is integer and increases by at least 1, the term with highest exponent will eventually increase; but could there be a case where the function is not strictly increasing? Consider string "10": value = base. For base=2, value=2; base=3, value=3, increasing. If the leading coefficient is positive, it's increasing. But if the highest non-zero digit is not at the highest exponent? Actually the highest exponent with non-zero digit determines the degree. The polynomial is sum d_i * base^i. For integer base, it's strictly increasing because if base1 < base2, then each term d_i * base1^i <= d_i * base2^i, and at least one term with i>=1 and d_i>0 has strictly larger value. So yes, strictly increasing.
Thus we can loop until break.
But we need to set an upper bound to avoid infinite loop in case the values never exceed bounds? That could happen if both strings are constant? But we already handled that. So for non-constant, eventually they will exceed bounds because they grow without bound. Since bounds are small, they will exceed soon. So we can just loop with while True and break when both non-constant values have exceeded bounds? Actually we need to stop when the condition fails permanently. But we can just loop up to some high number like 1000, but that is safe.
Given the small ranges, I'll just loop base from base_min to 60 (inclusive maybe 60) and check each.
But what about base_min > 60? Then the loop will not execute, and we output nothing -> 0. But we need to check if maybe there is a valid base >60? Could there be a valid base >60? Only if both strings are constant (infinite) or maybe if one string is constant and the other non-constant but somehow value <60 even for base >60? Non-constant value >= base, so impossible. So base>60 invalid. So if base_min > 60, then no valid base. So we can set upper bound to 60 (or 100) and if base_min > 60, just output 0.
But is 60 included? Base=60: For a non-constant string a, a >=60? Actually a must be <24, so a non-constant => base must be <24, so base_min <=23. So if base_min > 23, then a non-constant cannot have valid base. So for cases where a is non-constant and base_min >23, output 0. Similarly for b non-constant and base_min >59, output 0.
Thus we can set upper bound = 60, but loop from base_min to 60 inclusive, but check conditions and break if exceeding.
But careful: Base=60 might be valid if both strings are constant? But that's infinite and already handled. So not needed.
Thus implementation:
-
parse input, split on ':'.
-
convert each char to value.
-
compute max_digit = max(all digit values)
-
compute is_const_a, const_val_a (if is_const_a else None)
-
same for b.
-
If is_const_a and is_const_b:
if const_val_a < 24 and const_val_b < 60: print(-1)
else: print(0)
return.
-
If (is_const_a and const_val_a >= 24) or (is_const_b and const_val_b >= 60):
print(0)
return.
-
Now we have at least one non-constant, and we know that if a is constant, its value <24; if b constant, its value <60.
-
base_min = max(2, max_digit+1)
-
Initialize results list.
-
For base in range(base_min, 61): # up to 60 inclusive
compute value_a = evaluate(a_digits, base)
if value_a >= 24:
# if a is constant, this shouldn't happen because we already checked const_val_a <24. So a non-constant, and since it's increasing, break
break
compute value_b = evaluate(b_digits, base)
if value_b >= 60:
# if b constant, this shouldn't happen. So b non-constant, break
# but careful: maybe both a and b non-constant, but a might still be <24 while b >=60. Then break.
# but if b constant and b <60, then value_b will be constant <60, so never >=60. So break only if b non-constant.
# But we can simply: if value_b >= 60: break after checking? But we need to check both conditions; if value_b >=60, this base is invalid, and further bases will have larger b, so all invalid. So break.
# However, what if a is constant and b non-constant, and at base, a <24 but b >=60 -> invalid, and break.
# But break should be after we know for this base it's invalid? Actually we break the loop because for larger bases, b will only increase, so no chance of b<60. So break.
else:
if value_b < 60:
# both conditions satisfied
results.append(base)
But we also need to handle the case where a non-constant but b constant: a may exceed 24 while b still constant <60. So break when a>=24.
So we can put break conditions after computing a and b, but careful: if a>=24 we break immediately; if b>=60 we break; else add base.
-
After loop, output results as space-separated numbers, or 0 if empty.
But we must ensure that for a constant a, the value_a computed each loop is constant and will never become >=24, so we won't break early erroneously. So the break condition on a>=24 should only apply if a is non-constant? Actually if a is constant and we know it's <24, it will never become >=24. So the condition a>=24 will never be true, so safe to break if it happens (but it won't). However, what if a is constant but we earlier checked const_val_a >=24 and returned 0, so it won't happen.
Similarly for b.
Thus we can break when (value_a >= 24) or (value_b >= 60). But if a constant and <24, value_a never >=24. If b constant and <60, value_b never >=60. So it's safe.
But we must also consider that for a constant a but value_a <24, it's fine.
Now the loop range: from base_min to ... but we need an upper bound to stop if a and b never exceed bounds? For non-constant, they will exceed bounds eventually. But if both are constant? Already handled infinite. So the loop will eventually break because at some base, either a or b (non-constant) will exceed bound. However, if both are constant? Not in this branch. So we could loop indefinitely? For a constant and b constant? Not here. For constant a and non-constant b, b will exceed 60 at some base. What if b's value never exceeds 60? Since b is non-constant, it grows without bound, so eventually >60. But we need to ensure we iterate enough bases to see it exceed. Since b is polynomial with positive leading coefficient, for large base it will exceed 60. But we need to set an upper bound to avoid infinite loop if we don't break? We can use while True, but we must guarantee termination. As argued, b >= base, so for base >= 60, b >= 60, so at base=60, b >=60. So we can set upper bound as 60, because as soon as base reaches 60, if b is non-constant, b >= 60, and the base is invalid. But what if b's leading coefficient is >1? Then b might exceed 60 earlier, but still, at base=60 it's definitely invalid. So if we iterate up to 60, we will cover all bases less than 60 that might be valid. For base=60, it's invalid because b>=60? But could b be exactly 60 and be invalid? minutes must be <60, so 60 is not allowed. So base=60 invalid. So the maximum base that could be valid is 59. So we can set loop from base_min to 60 (exclusive) i.e., up to 59. But careful: Could there be a valid base >59 if b is constant? That would be infinite case, handled earlier. So no. So we can set range(base_min, 60). Actually if base_min = 59 and we check base=59, then base=60 is not checked. That's fine because base=60 would be invalid anyway. So we can set upper bound = 60, but not include 60: for base in range(base_min, 60). Since range stops at 60 exclusive, base goes up to 59. That's sufficient.
But what about the case where base_min = 60? That would mean max_digit >= 59, but max_digit max is 35, so base_min max is 36. So base_min <=36, so always <=36. So range(base_min, 60) is fine.
Thus the loop is small.
Now we need to be careful about the constant detection: is_const = all digits except last are zero. That includes strings of length 1 (no digits before last, vacuously true). So for length 1, it's constant. Indeed "1" yields constant value 1. "A" yields constant value 10. So infinite if both constant and within bounds.
But also need to handle strings that are all zeros: then last digit is zero, and all except last are zero (since all are zero). So is_const = True, const_val = 0. That's constant zero.
Now, one more nuance: The problem statement says: "Note that on Mars any positional numeral systems with positive radix strictly larger than one are possible." So base must be integer >=2.
Now, test examples:
Example 1: "11:20"
a="11", b="20"
Digits: a: [1,1] -> max_digit=1 -> base_min=2.
is_const_a? digits except last: first digit 1 !=0 => not constant. is_const_b? b digits: '2','0' -> first digit 2 !=0 => not constant.
So loop base from 2 to 59.
Compute:
base=2: a = 12+1=3 <24, b=22+0=4 <60 -> valid.
base=3: a=13+1=4, b=23+0=6 -> valid.
...
base=20: a=120+1=21, b=220+0=40 -> valid.
base=21: a=22, b=42 -> a=22<24, b=42<60 -> valid? a=22<24? yes 22<24, b=42<60. So base=21 valid.
base=22: a=23, b=44 -> a=23<24, b=44<60 -> valid.
base=23: a=24, b=46 -> a=24 not <24 => invalid. So valid bases: 2..22. But example output: 3 4 5 ... 22? Wait example output: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22. That's from 3 to 22. But we have base=2 also valid? Let's compute a for base=2: "11" in base 2? Digits are 1 and 1, but base must be > max digit =1, so base>=2 is okay. In base 2, the digit '1' is valid. So "11" in base 2 = 1*2 + 1 = 3, which is less than 24. "20" in base 2: digit '2' is not valid because 2 >= base. Wait! For base=2, max digit in b is 2? b="20" contains digit '2'. 2 is not <2. So base=2 invalid because digit '2' is not allowed in base 2. Our base_min = max(2, max_digit+1). max_digit among a and b: a max digit=1, b max digit=2, so max_digit=2, so base_min = max(2, 3) = 3. So base_min=3, not 2. I missed that: we must ensure all digits are less than base. For base=2, digit '2' in b is invalid. So base_min must be at least max_digit+1. So base_min=3. So that matches example: start from 3. So base_min calculation should use max digit across both strings. Already we computed max_digit = max of all digits. So base_min = max(2, max_digit+1). In first sample, max_digit = max(1,2)=2 => base_min=3. So our loop from base_min to 59 yields bases 3..22. That matches example.
Now Example 2: "2A:13"
a="2A": digits: 2,10 (A=10) => max_digit=10. b="13": digits:1,3 => max_digit_b=3, overall max=10 => base_min=11.
is_const_a? digits except last: first digit 2 !=0 => not constant. is_const_b? digits except last: first digit 1 !=0 => not constant.
Loop base from 11 to 59. Compute value_a and b.
Check base=11: a = 211+10=32, >=24 -> invalid. So break? Since a is non-constant, once a>=24 we break. But we need to check all bases? Because a could be less than 24 for some base >11? Let's compute: a = 2base + 10. For base=11: 32 >24. As base increases, a increases, so never <24. So no valid base. Output 0. That matches example.
But we break after base=11 because a>=24, and since a is non-constant and increasing, further bases will have larger a, so no need to continue. So results empty -> print 0.
Example 3: "000B:00001"
a="000B": digits: 0,0,0,11? Actually B=11. So digits: [0,0,0,11]? Wait length 4: positions: from leftmost (most significant) to rightmost: index 3 (most),2,1,0. So digits: d3=0,d2=0,d1=0,d0=11. is_const_a? All except last are zero -> yes. const_val_a = 11.
b="00001": digits: length 5: d4=0,d3=0,d2=0,d1=0,d0=1 -> all except last zero -> constant, const_val_b=1.
max_digit = max(11,1)=11 -> base_min=12.
Both constant, and const_val_a=11<24, const_val_b=1<60 => infinite => output -1. That matches example.
Now we need to think about edge cases.
Edge case: "0:0". Both constant zero. const_val_a=0<24, const_val_b=0<60 -> infinite => -1. But is that correct? base_min = max(2, max_digit+1). max_digit=0 => base_min=2. For any base >=2, digits valid, hours=0, minutes=0 => valid. So infinite.
Edge case: "0:1". a constant 0, b constant 1? b is "1", length 1, constant? Actually length 1, all except last (none) vacuously true, so constant, const_val_b=1. Both constant => infinite? But check: a constant 0 (<24), b constant 1 (<60). So infinite. For any base >= max_digit+1? max_digit = max(0,1)=1 => base_min=2. For any base >=2, a=0, b=1 => valid. So infinite => -1. Is that correct? Let's think: "0:1" in any base >=2, minutes = 1, hours =0, valid. So yes infinite.
Edge case: "0:3C"? b="3C", digits: 3 and 12 (C). b not constant because first digit 3 !=0. So a constant 0. b non-constant. Then we need to find bases where b<60. b = 3base + 12. Solve 3base+12 <60 => 3base <48 => base <16. Also base >= max_digit+1 = max(0,12)+1=13? Actually max_digit=12 => base_min=13. So base from 13 to 15 inclusive check: base=13: b=313+12=51 <60 valid. base=14: 3*14+12=54 valid. base=15: 57 valid. base=16: 60 not valid. So results: 13,14,15. That seems fine.
Edge case: "2:0". a constant 2 (<24), b constant 0 => both constant => infinite? Actually both constant? a "2" constant, b "0" constant. Both constant, within bounds => infinite. So output -1. For any base >= max_digit+1 = max(2,0)+1=3? Actually max_digit=2 => base_min=3. But base=2? In base 2, digit '2' is invalid because 2 >=2? Actually digit must be < base. So base must be >2. So base_min=3. For base=3, a=2, b=0 valid. For any base>=3, a=2, b=0 valid. So infinite.
But what about "23:0"? a constant 23 (<24), b constant 0 => both constant, so infinite? Actually a=23, digit '2' and '3'? Wait "23" length 2, digits: '2' and '3'. Are both constant? Check is_const_a: all digits except last must be zero. The first digit is '2' not zero, so not constant. So a is not constant. So it's not infinite. So a non-constant. Compute: base_min = max(2, max(2,3)+1)=4? Actually max_digit = 3 (digit '3'), base_min = 4. For base=4: a=24+3=11, b=0 => valid? b constant 0, valid. base=5: a=13, base=6:15, base=7:17, base=8:19, base=9:21, base=10:23, base=11: 211+3=25 >=24 invalid. So valid bases 4-10 inclusive. That's finite. So correct.
Now consider a constant that is out of bounds: "24:0". a constant? "24": digits '2','4', first digit not zero, so not constant. So finite. But consider a constant like "24"? Actually length 2, first digit not zero => non-constant. So constant only if all but last zero. So "24" is non-constant. So constant out-of-bound would be something like "30" but "30" has first digit 3 not zero => non-constant. So constant out-of-bound could be a single digit like "24" but digit '24'? Digit values go up to 35, so "24" as a string of two characters: '2' and '4', not a single digit. So constant out-of-bound would be a single digit >23? For example, "24"? That's two digits, so not constant. So constant out-of-bound only if the constant value >=24. That can happen with a single digit like "1A"? Actually "1A" is two digits, not constant. Single digit can be up to 'Z'=35, so a constant a = 30 (like "U") would be >=24. Then infinite? But both constant? If b also constant and within bounds? Actually if a constant 30 (>=24), then for any base >30, a=30 which is not <24, so no base works. So output 0. So our logic: if both constant, we check bounds, if not both within, output 0. That's correct.
But what about case where a constant 30 and b non-constant? Then early check: if is_const_a and const_val_a >=24 -> print 0. So correct.
Now, we must also consider case where a constant 5 and b non-constant, but base_min might be high such that b's value already >=60 at base_min, but maybe at some higher base? Actually b non-constant increases with base, so if at base_min b>=60, then for larger bases it will be even larger, so no valid base. So our loop will check base_min, see b>=60, then break and output nothing. But we need to also check if maybe at base_min b<60 but later b becomes >=60 and then a still constant and valid. That's covered by loop.
Now, we also need to consider that for a non-constant a, its value may be less than 24 at base_min, then increase, exceed 24 at some base, and then for even larger base, could it become less than 24 again? No, because it's strictly increasing. So break when a>=24 is safe.
Similarly for b.
Now, one more nuance: When a is non-constant but b is constant, we break when a>=24. However, b constant and <60, so no break due to b. So loop will continue until a>=24. But we also must consider that base must be at least base_min and integer. So loop from base_min to maybe 60, but if a never exceeds 24 for bases up to 59? But a non-constant: as base increases, a grows; eventually it will exceed 24. Since a >= base (if leading non-zero digit at exponent>=1), base can't exceed 23. So a will exceed 24 by base=24 at latest (if coefficient 1). So loop to 59 is enough.
But what if a non-constant but its leading digit is zero? Actually if a is non-constant, there is some digit at position i>=1 that is non-zero. The smallest possible such non-zero digit is 1 at position 1 (the second from right). So a >= base. So for base >=24, a>=24 => invalid. So maximum base for a non-constant is 23. So we could break earlier.
Now for b non-constant, maximum base is 59. So loop to 60 is safe.
Thus implementation seems straightforward.
But we must be careful about evaluating the value for a given base. Since the strings length up to 5, we can compute by iterating digits from left to right: value = 0; for each char: value = value * base + digit. That's the standard conversion.
Now we need to check digit validity: we already ensure base > max_digit, but what about digits that are less than base? Since base > max_digit, all digits are < base. So conversion is valid.
Edge Cases: Input may have leading zeros, but that's fine.
Now test some more:
Case: "0:0" -> infinite.
Case: "00:00" -> same.
Case: "1:60"? Actually minutes "60" is length 2, digits 6 and 0. is_const_b? first digit 6 !=0 => not constant. b value = 6base + 0 = 6base. For base_min? max_digit = max(1? Actually a "1": digit 1, b "60": digits 6 and 0, max_digit=6 => base_min=7. For base=7: a=1, b=42 <60 valid. base=8: b=48, base=9: b=54, base=10: b=60 not <60 (equal, invalid). So valid bases 7,8,9. That seems right.
Case: "0:0Z"? b="0Z": digits: 0, 35. is_const_b? first digit 0 => constant? Actually all except last zero: first digit is 0, last digit is 35. So constant, const_val_b=35. a constant 0. Both constant? a constant yes, b constant yes. Check bounds: a=0<24, b=35<60 => infinite? Wait const_val_b=35 <60, yes. So infinite? But b = "0Z" is constant 35. So for any base >35? Actually base must be > max_digit = max(0,35)=35 => base_min=36. For base >=36, b=35, a=0 => valid. So infinite many bases >=36. Indeed output -1. That's correct.
But what about base=35? base=35 is not allowed because digit Z=35 is not <35 (must be strictly less). So base_min=36. So infinite many bases from 36 onward. So -1.
Now check another case: "0:1Z"? b="1Z": digits 1, 35. Not constant because first digit 1 !=0. So a constant 0. max_digit=35 => base_min=36. For base=36: a=0, b=1*36+35=71 >=60 -> invalid. Since b is non-constant, increasing, for base=36 it's already >=60. So no bases. So output 0.
Now what about "10:0"? a="10" non-constant, b="0" constant 0. max_digit = max(1,0)=1? Actually a digits: 1 and 0 => max_digit=1, b digits: 0 => max_digit=0, overall max=1 => base_min=2. For base=2: a=1*2+0=2 <24, b=0 valid. base=3: a=3, base=4:4,... up to base=23: a=23? Actually a = base. So for base=23, a=23 valid. base=24: a=24 invalid. So valid bases 2..23. But base_min=2, so include base=2. That seems correct. But wait, b constant 0 is fine. So outputs 2 3 ... 23. That is valid.
Now consider "0:10"? a constant 0, b non-constant "10". max_digit = max(0,1)=1? Actually b digits: 1 and 0 => max_digit=1, base_min=2. b = base. Need b < 60 => base < 60. So valid bases 2..59. But also a constant 0 valid. So results 2..59. That's finite. But is that correct? b = base, so minutes = base. For base=59, minutes=59 <60 valid. For base=60, minutes=60 invalid. So finite.
But wait, is there any scenario where both strings are non-constant but still infinite? That would require that for arbitrarily large base, both a<24 and b<60. But as argued, if non-constant, a >= base, so base must be <24; so base bounded by 23. So finite.
Thus algorithm seems correct.
Now, there is one more nuance: The input string may have digits that are not valid in any base? For example, if a contains a digit that is 1, then base must be at least 2, which is fine. But what about a digit like '0'? That's fine. So base_min is at least 2.
Now, we must also consider the case where a or b is an empty string? Not possible; length at least 1.
Now implement.
Potential pitfalls:
- When computing is_const, we need to check all characters except the last. For string length 1, there are no characters except the last, so is_const true. That's correct.
- For all-zero string, the last digit is '0', and all preceding are '0', so is_const true, const_val=0.
- When evaluating the value, we need to be careful about overflow? Values may become large for large base? But we only evaluate up to base=59, and length max 5, so max value: for base=59, digits up to 35, length 5, max possible number: 35*59^4 + ... but 59^4 ≈ 12 million, times 35 ~ 400 million, still fits in 64-bit integer. Actually 59^4 = (59^2)^2 = 3481^2 ≈ 12,117,361; times 35 ≈ 424 million. 59^5 ~ 714 million, times 35 ~ 25 billion. Still fits in 64-bit (max ~9e18). So we can use Python int, which is arbitrary precision, so no problem.
But we could also break early if value exceeds bound, to avoid unnecessary computation. However not needed.
Now what about base_min maybe > 60? As argued, max_digit <=35, so base_min <=36, so always <=36, so loop from base_min to 60 is fine.
But wait, what about the possibility that max_digit = 35, base_min=36. For non-constant strings, base_min=36 > 23 maybe, but a non-constant a would then be invalid because a must be <24 but base_min=36 gives a>=36? Actually we will loop from 36 to 59, but a non-constant will be >=36, so invalid. So results empty.
Now, we need to ensure we handle the case where a is constant but value >=24, we early output 0. Similarly for b.
Now implement steps:
-
Read line, strip newline.
-
Split by ':'.
-
a_str, b_str = parts[0], parts[1].
-
Function to process string s:
- digits = list of ints using mapping: '0'-'9' -> 0-9, 'A'-'Z' -> 10-35.
- max_digit = max(digits)
- is_const = all(d == 0 for d in digits[:-1]) if len(s)>1 else True (since len=1 => is_const True)
- const_val = digits[-1] if is_const else None (or 0)
- return digits, max_digit, is_const, const_val
-
Get these for a and b.
-
max_digit_overall = max(max_digit_a, max_digit_b)
-
base_min = max(2, max_digit_overall + 1)
-
if is_const_a and is_const_b:
if const_val_a < 24 and const_val_b < 60:
print(-1)
else:
print(0)
return
-
if (is_const_a and const_val_a >= 24) or (is_const_b and const_val_b >= 60):
print(0)
return
Actually if one constant and out-of-range, no bases, so return 0.
-
results = []
for base in range(base_min, 60): # 60 exclusive, i.e., up to 59
# evaluate a
val_a = evaluate(digits_a, base)
if val_a >= 24:
break # because a non-constant? but if constant, we already returned, so a must be non-constant or constant but <24. If constant, val_a constant <24, so break never reached. So safe.
val_b = evaluate(digits_b, base)
if val_b >= 60:
# b non-constant? if constant we already returned, so must be non-constant. So break.
break
# both valid
results.append(base)
But we need to consider the possibility that a is constant and b non-constant, and at some base b becomes >=60, we break. That's correct.
However, what about the scenario where a is non-constant and b constant? Then break condition for a will trigger, and we break. That's fine.
But wait: if a is non-constant and b constant, and a becomes >=24 at some base, we break. But what if for a later base, a could become <24 again? Not possible, monotonic.
So after loop, output results.
But is it safe to break as soon as we encounter a base where val_a >= 24 or val_b >= 60? Yes, because for subsequent bases (larger), both val_a (if non-constant) and val_b (if non-constant) will be larger (or equal for constant). For constant values, they are below bounds, so the break condition would not be triggered by them because we already early returned if constant out-of-range. So for constants, they remain below bounds, so they won't cause break. So break only when the variable that caused the condition is non-constant, and for larger bases it will be even larger, so no further bases can satisfy. So break is correct.
But what