-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10_Count_the_Number_of_Powerful_Integers.cpp
111 lines (90 loc) · 3.92 KB
/
10_Count_the_Number_of_Powerful_Integers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 2999. Count the Number of Powerful Integers
// You are given three integers start, finish, and limit. You are also given a 0-indexed string s representing a positive integer.
// A positive integer x is called powerful if it ends with s (in other words, s is a suffix of x) and each digit in x is at most limit.
// Return the total number of powerful integers in the range [start..finish].
// A string x is a suffix of a string y if and only if x is a substring of y that starts from some index (including 0) in y and extends to the index y.length - 1. For example, 25 is a suffix of 5125 whereas 512 is not.
// Example 1:
// Input: start = 1, finish = 6000, limit = 4, s = "124"
// Output: 5
// Explanation: The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
// It can be shown that there are only 5 powerful integers in this range.
// Example 2:
// Input: start = 15, finish = 215, limit = 6, s = "10"
// Output: 2
// Explanation: The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.
// It can be shown that there are only 2 powerful integers in this range.
// Example 3:
// Input: start = 1000, finish = 2000, limit = 4, s = "3000"
// Output: 0
// Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.
// Constraints:
// 1 <= start <= finish <= 1015
// 1 <= limit <= 9
// 1 <= s.length <= floor(log10(finish)) + 1
// s only consists of numeric digits which are at most limit.
// s does not have leading zeros.
long long tens[17];
long long powB[17];
class Solution
{
public:
void compute_tens()
{
if (tens[0] == 1)
return;
tens[0] = 1;
for (int i = 1; i < 17; i++)
tens[i] = tens[i - 1] * 10LL;
}
void compute_powB(int B)
{
powB[0] = 1;
for (int i = 1; i < 17; i++)
powB[i] = powB[i - 1] * B;
}
long long cnt(long long x, long long sn, int ds, int limit)
{
if (x < sn)
return 0;
long long x0 = x, rem = x0 % tens[ds];
x0 /= tens[ds];
int d = lower_bound(tens, tens + 17, x0) - tens;
long long cnt = 0;
for (int i = d; i >= 0; i--)
{ // MSDF
auto [q, r] = ldiv(x0, tens[i]);
if (q > limit)
{
cnt += powB[i + 1];
return cnt;
}
cnt += powB[i] * q;
x0 -= q * tens[i];
}
return cnt + (rem >= sn);
}
long long numberOfPowerfulInt(long long start, long long finish, int limit, string &s)
{
compute_tens();
compute_powB(limit + 1);
long long sn = stoll(s);
int ds = s.size(), d0 = log10(start) + 1, d1 = log10(finish) + 1;
return cnt(finish, sn, ds, limit) - cnt(start - 1, sn, ds, limit);
}
};
/*
This code solves the problem of counting powerful integers in a given range. Here's how it works:
1. The code uses two arrays 'tens' and 'powB' to precompute powers of 10 and powers of (limit+1) respectively.
2. compute_tens(): Fills the tens array with powers of 10 (1, 10, 100, ...)
compute_powB(): Fills the powB array with powers of (limit+1)
3. cnt() function:
- Counts powerful integers up to number x
- Uses Most Significant Digit First (MSDF) approach
- Processes each digit position and accumulates count based on limit constraints
- Handles the suffix requirement using modulo operation
4. numberOfPowerfulInt():
- Main function that uses cnt() to find the difference between counts
- Converts string suffix to number and handles range calculations
Time Complexity: O(log N) where N is the maximum number (finish)
Space Complexity: O(1) as arrays are fixed size (17)
*/