|
| 1 | +package g3501_3600.s3519_count_numbers_with_non_decreasing_digits; |
| 2 | + |
| 3 | +// #Hard #String #Dynamic_Programming #Math #2025_04_14_Time_19_ms_(100.00%)_Space_45.43_MB_(50.00%) |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class Solution { |
| 9 | + public int countNumbers(String l, String r, int b) { |
| 10 | + long ans1 = find(r.toCharArray(), b); |
| 11 | + char[] start = subTractOne(l.toCharArray()); |
| 12 | + long ans2 = find(start, b); |
| 13 | + return (int) ((ans1 - ans2) % 1000000007L); |
| 14 | + } |
| 15 | + |
| 16 | + private long find(char[] arr, int b) { |
| 17 | + int[] nums = convertNumToBase(arr, b); |
| 18 | + Long[][][] dp = new Long[nums.length][2][11]; |
| 19 | + return solve(0, nums, 1, b, 0, dp) - 1; |
| 20 | + } |
| 21 | + |
| 22 | + private long solve(int i, int[] arr, int tight, int base, int last, Long[][][] dp) { |
| 23 | + if (i == arr.length) { |
| 24 | + return 1L; |
| 25 | + } |
| 26 | + if (dp[i][tight][last] != null) { |
| 27 | + return dp[i][tight][last]; |
| 28 | + } |
| 29 | + int till = base - 1; |
| 30 | + if (tight == 1) { |
| 31 | + till = arr[i]; |
| 32 | + } |
| 33 | + long ans = 0; |
| 34 | + for (int j = 0; j <= till; j++) { |
| 35 | + if (j >= last) { |
| 36 | + ans = (ans + solve(i + 1, arr, tight & (j == arr[i] ? 1 : 0), base, j, dp)); |
| 37 | + } |
| 38 | + } |
| 39 | + return dp[i][tight][last] = ans; |
| 40 | + } |
| 41 | + |
| 42 | + private char[] subTractOne(char[] arr) { |
| 43 | + int n = arr.length; |
| 44 | + int i = n - 1; |
| 45 | + while (i >= 0 && arr[i] == '0') { |
| 46 | + arr[i--] = '9'; |
| 47 | + } |
| 48 | + int x = arr[i] - '0' - 1; |
| 49 | + arr[i] = (char) (x + '0'); |
| 50 | + int j = 0; |
| 51 | + int idx = 0; |
| 52 | + while (j < n && arr[j] == '0') { |
| 53 | + j++; |
| 54 | + } |
| 55 | + char[] res = new char[n - j]; |
| 56 | + for (int k = j; k < n; k++) { |
| 57 | + res[idx++] = arr[k]; |
| 58 | + } |
| 59 | + return res; |
| 60 | + } |
| 61 | + |
| 62 | + private int[] convertNumToBase(char[] arr, int base) { |
| 63 | + int n = arr.length; |
| 64 | + int[] num = new int[n]; |
| 65 | + int i = 0; |
| 66 | + while (i < n) { |
| 67 | + num[i] = arr[i++] - '0'; |
| 68 | + } |
| 69 | + List<Integer> temp = new ArrayList<>(); |
| 70 | + int len = n; |
| 71 | + while (len > 0) { |
| 72 | + int rem = 0; |
| 73 | + int[] next = new int[len]; |
| 74 | + int newLen = 0; |
| 75 | + int j = 0; |
| 76 | + while (j < len) { |
| 77 | + long cur = rem * 10L + num[j]; |
| 78 | + int q = (int) (cur / base); |
| 79 | + rem = (int) (cur % base); |
| 80 | + if (newLen > 0 || q != 0) { |
| 81 | + next[newLen] = q; |
| 82 | + newLen++; |
| 83 | + } |
| 84 | + j++; |
| 85 | + } |
| 86 | + temp.add(rem); |
| 87 | + num = next; |
| 88 | + len = newLen; |
| 89 | + } |
| 90 | + int[] res = new int[temp.size()]; |
| 91 | + int k = 0; |
| 92 | + int size = temp.size(); |
| 93 | + while (k < size) { |
| 94 | + res[k] = temp.get(size - 1 - k); |
| 95 | + k++; |
| 96 | + } |
| 97 | + return res; |
| 98 | + } |
| 99 | +} |
0 commit comments