You are given a string num which represents a positive integer, and an integer t.
A number is called zero-free if none of its digits are 0.
Return a string representing the smallestzero-free number greater than or equal to num such that the product of its digits is divisible by
t. If no such number exists, return "-1".
Input: num ="1234", t =256Output: "1488"Explanation:
The smallest zero-free number that is greater than 1234 and has the product of
its digits divisible by 256is1488,with the product of its digits equal to
256.
Input: num ="12355", t =50Output: "12355"Explanation:
12355is already zero-free and has the product of its digits divisible by 50,with the product of its digits equal to 150.
We need the smallest zero-free number >= num whose digit product is divisible by t. For small t, we can use digit DP with memoization. For large t, the problem is very hard in general, but for practical constraints, we can use greedy and pruning.
classSolution:
defsmallestNumber(self, num: str, t: int) -> str:
from functools import lru_cache
n = len(num)
digits = [int(c) for c in num]
@lru_cache(maxsize=None)
defdp(i, tight, prod):
if i == n:
return''if prod % t ==0elseNone start = digits[i] if tight else1for d in range(start, 10):
if d ==0: continue res = dp(i+1, tight and d == digits[i], (prod * d) % t)
if res isnotNone:
return str(d) + res
returnNone ans = dp(0, True, 1)
if ans isnotNone:
return ans
# Try to increase the number by changing the first non-9 digitfor i in range(n-1, -1, -1):
if num[i] !='9'and num[i] !='0':
prefix = num[:i] + str(int(num[i])+1)
suffix ='1'* (n-i-1)
candidate = prefix + suffix
prod =1for c in candidate:
prod *= int(c)
if prod % t ==0:
return candidate
return"-1"
// Java solution is not practical for large t, but for small t, use digit DP with memoization.import java.util.*;
classSolution {
public String smallestNumber(String num, int t) {
int n = num.length();
int[] digits =newint[n];
for (int i = 0; i < n; ++i) digits[i]= num.charAt(i) -'0';
Map<String, String> memo =new HashMap<>();
String res = dp(0, true, 1, digits, t, memo);
if (res !=null) return res;
// Greedy fallback: try to increment the first non-9 digitfor (int i = n-1; i >= 0; --i) {
if (digits[i]!= 9 && digits[i]!= 0) {
StringBuilder sb =new StringBuilder();
for (int j = 0; j < i; ++j) sb.append(digits[j]);
sb.append(digits[i]+1);
for (int j = i+1; j < n; ++j) sb.append('1');
String candidate = sb.toString();
long prod = 1;
for (char c : candidate.toCharArray()) prod *= c -'0';
if (prod % t == 0) return candidate;
}
}
return"-1";
}
private String dp(int i, boolean tight, int prod, int[] digits, int t, Map<String, String> memo) {
if (i == digits.length) return prod % t == 0 ?"" : null;
String key = i+","+tight+","+prod;
if (memo.containsKey(key)) return memo.get(key);
int start = tight ? digits[i] : 1;
for (int d = start; d <= 9; ++d) {
if (d == 0) continue;
String res = dp(i+1, tight && d == digits[i], (prod * d) % t, digits, t, memo);
if (res !=null) {
memo.put(key, d + res);
return d + res;
}
}
memo.put(key, null);
returnnull;
}
}