3855. Sum of K-Digit Numbers in a Range Hard

@problem@discussion



1/**
2 * [3855] Sum of K-Digit Numbers in a Range
3 *
4 * You are given three integers l, r, and k.
5 * Consider all possible integers consisting of exactly k digits, where each digit is chosen independently from the integer range [l, r] (inclusive). If 0 is included in the range, leading zeros are allowed.
6 * Return an integer representing the sum of all such numbers.​​​​​​​ Since the answer may be very large, return it modulo 10^9 + 7.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">l = 1, r = 2, k = 2</span>
11 * Output: <span class="example-io">66</span>
12 * Explanation:
13 * 
14 * 	All numbers formed using k = 2 digits in the range [1, 2] are 11, 12, 21, 22.
15 * 	The total sum is 11 + 12 + 21 + 22 = 66.
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">l = 0, r = 1, k = 3</span>
20 * Output: <span class="example-io">444</span>
21 * Explanation:
22 * 
23 * 	All numbers formed using k = 3 digits in the range [0, 1] are 000, 001, 010, 011, 100, 101, 110, 111​​​​​​​.
24 * 	These numbers without leading zeros are 0, 1, 10, 11, 100, 101, 110, 111.
25 * 	The total sum is 444.
26 * </div>
27 * <strong class="example">Example 3:
28 * <div class="example-block">
29 * Input: <span class="example-io">l = 5, r = 5, k = 10</span>
30 * Output: <span class="example-io">555555520</span>
31 * Explanation:​​​​​​​
32 * 
33 * 	5555555555 is the only valid number consisting of k = 10 digits in the range [5, 5].
34 * 	The total sum is 5555555555 % (10^9 + 7) = 555555520.
35 * </div>
36 *  
37 * Constraints:
38 * 
39 * 	0 <= l <= r <= 9
40 * 	1 <= k <= 10^9
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/sum-of-k-digit-numbers-in-a-range/
46// discuss: https://leetcode.com/problems/sum-of-k-digit-numbers-in-a-range/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn sum_of_numbers(l: i32, r: i32, k: i32) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_3855() {
64    }
65}
66

Back
© 2026 bowen.ge All Rights Reserved.