|
1 |
| -Let us precompute all lucky numbers smaller than 10^5. |
2 |
| -
|
3 |
| -There are not many lucky numbers in this range. |
4 |
| -At most 14. |
5 |
| -
|
6 |
| ------ |
7 |
| -
|
8 |
| -We want to count the number of integers where the frequency of lucky digits is a lucky number. |
9 |
| -
|
10 |
| -We will do this with a digit DP. |
11 |
| -
|
12 |
| -Let f(N) be the number of lucky integers strictly < N |
13 |
| -
|
14 |
| -Then, the answer is f(R + 1) - f(L) |
15 |
| -
|
16 |
| ------ |
17 |
| -
|
18 |
| -Now, let us discuss how to calculate f(N) using Digit DP |
19 |
| -
|
20 |
| -
|
21 |
| -First, let us count the number of lucky integers without leading 0 |
22 |
| -
|
23 |
| -Every integer of this form will match some prefix of length N, will then have a smaller integer |
24 |
| -and can then have any suffix. |
25 |
| -
|
26 |
| -Let us iterate over every prefix of length [0, i] |
27 |
| -We will then make D[i + 1] < S[i + 1] |
28 |
| -And keep track of the total number of lucky integers in the prefix so far. |
29 |
| -
|
30 |
| -Let us say it is f. |
31 |
| -The final frequency of lucky integers should be one of the 14 lucky numbers. |
32 |
| -Suppose the final frequency is F and (F - f) > suffix length. |
33 |
| -
|
34 |
| -There are two ways to choose a lucky digit and 8 ways to choose a normal digit. |
35 |
| -
|
36 |
| -The number of ways to create the suffix is Choose(Suffix, F - f) x 2^{F - f} x 8^{Suffix - (F - f)} |
37 |
| -
|
38 |
| ------ |
39 |
| -
|
40 |
| -for(int i = 0; i < N.size(); i++) |
41 |
| - { |
42 |
| - int remaining_digits = (N.size() - 1) - i; |
43 |
| - for(int current_digit = 0; current_digit < N[i] - '0'; current_digit++) |
44 |
| - { |
45 |
| - if(i == 0 && current_digit == 0) |
46 |
| - { |
47 |
| - continue; |
48 |
| - } |
49 |
| - for(auto lucky_frequency : lucky) |
50 |
| - { |
51 |
| - int lucky_prefix_frequency = |
52 |
| - prefix_frequency[4] + prefix_frequency[7] |
53 |
| - + (current_digit == 4 || current_digit == 7); |
54 |
| - |
55 |
| - if(lucky_frequency < lucky_prefix_frequency || |
56 |
| - lucky_frequency > lucky_prefix_frequency + remaining_digits) |
57 |
| - { |
58 |
| - continue; |
59 |
| - } |
60 |
| - |
61 |
| - int frequency_4 = prefix_frequency[4] + (current_digit == 4); |
62 |
| - int frequency_7 = prefix_frequency[7] + (current_digit == 7); |
63 |
| - |
64 |
| - int lucky_suffix = lucky_frequency - frequency_4 - frequency_7; |
65 |
| - |
66 |
| - long long choosing_lucky_suffix = choose(remaining_digits, lucky_suffix); |
67 |
| - choosing_lucky_suffix *= power(2, lucky_suffix, MOD); |
68 |
| - choosing_lucky_suffix %= MOD; |
69 |
| - |
70 |
| - int non_lucky_suffix = remaining_digits - lucky_suffix; |
71 |
| - long long choosing_non_lucky_suffix = power(8, non_lucky_suffix, MOD); |
72 |
| - |
73 |
| - long long count_here = choosing_lucky_suffix*choosing_non_lucky_suffix; |
74 |
| - count_here %= MOD; |
75 |
| - |
76 |
| - lucky_count += count_here; |
77 |
| - lucky_count %= MOD; |
78 |
| - |
79 |
| - //cout << "N = " << N << " i = " << i << " d = " << current_digit << " Lucky " << lucky_frequency << "\nLucky Count = " << choosing_lucky_suffix << " others = " << choosing_non_lucky_suffix << " count = " << count_here << "\n"; |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - prefix_frequency[N[i] - '0']++; |
84 |
| - } |
85 |
| -
|
86 |
| ------ |
87 |
| -
|
88 |
| -The number of integers with leading 0s is similar except we do not have to match any prefix. |
89 |
| -
|
90 |
| -Just count free suffixes and make sure the final length is strictly smaller than the length of N |
91 |
| -
|
92 |
| ------ |
93 |
| -
|
94 |
| -for(int i = 1; i < N.size(); i++) |
95 |
| - { |
96 |
| - for(int d = 1; d <= 9; d++) |
97 |
| - { |
98 |
| - int remaining_digits = (N.size() - 1) - i; |
99 |
| - for(auto lucky_frequency : lucky) |
100 |
| - { |
101 |
| - int lucky_here = (d == 4 || d == 7); |
102 |
| - if(lucky_frequency > lucky_here + remaining_digits) |
103 |
| - { |
104 |
| - continue; |
105 |
| - } |
106 |
| - |
107 |
| - int lucky_suffix_leading_0 = lucky_frequency - (d == 4 || d == 7); |
108 |
| - |
109 |
| - long long choosing_lucky_suffix_leading_0 = choose(remaining_digits, lucky_suffix_leading_0); |
110 |
| - choosing_lucky_suffix_leading_0 *= power(2, lucky_suffix_leading_0, MOD); |
111 |
| - choosing_lucky_suffix_leading_0 %= MOD; |
112 |
| - |
113 |
| - int non_lucky_suffix_leading_0 = remaining_digits - lucky_suffix_leading_0; |
114 |
| - long long choosing_non_lucky_suffix_leading_0 = power(8, non_lucky_suffix_leading_0, MOD); |
115 |
| - |
116 |
| - long long count_here_leading_0 = choosing_lucky_suffix_leading_0*choosing_non_lucky_suffix_leading_0; |
117 |
| - count_here_leading_0 %= MOD; |
118 |
| - |
119 |
| - lucky_count += count_here_leading_0; |
120 |
| - lucky_count %= MOD; |
121 |
| - |
122 |
| - //cout << "N0 = " << N << " i = " << i << " d = " << d << " Lucky " << lucky_frequency << "\nLucky Count = " << choosing_lucky_suffix_leading_0 << " others = " << choosing_non_lucky_suffix_leading_0 << " count = " << count_here_leading_0 << "\n"; |
123 |
| - } |
124 |
| - } |
125 |
| - } |
| 1 | +Let us precompute all lucky numbers smaller than 10^5. |
| 2 | + |
| 3 | +There are not many lucky numbers in this range. |
| 4 | +At most 14. |
| 5 | + |
| 6 | +----- |
| 7 | + |
| 8 | +We want to count the number of integers where the frequency of lucky digits is a lucky number. |
| 9 | + |
| 10 | +We will do this with a digit DP. |
| 11 | + |
| 12 | +Let f(N) be the number of lucky integers strictly < N |
| 13 | + |
| 14 | +Then, the answer is f(R + 1) - f(L) |
| 15 | + |
| 16 | +----- |
| 17 | + |
| 18 | +Now, let us discuss how to calculate f(N) using Digit DP |
| 19 | + |
| 20 | + |
| 21 | +First, let us count the number of lucky integers without leading 0 |
| 22 | + |
| 23 | +Every integer of this form will match some prefix of length N, will then have a smaller integer |
| 24 | +and can then have any suffix. |
| 25 | + |
| 26 | +Let us iterate over every prefix of length [0, i] |
| 27 | +We will then make D[i + 1] < S[i + 1] |
| 28 | +And keep track of the total number of lucky integers in the prefix so far. |
| 29 | + |
| 30 | +Let us say it is f. |
| 31 | +The final frequency of lucky integers should be one of the 14 lucky numbers. |
| 32 | +Suppose the final frequency is F and (F - f) > suffix length. |
| 33 | + |
| 34 | +There are two ways to choose a lucky digit and 8 ways to choose a normal digit. |
| 35 | + |
| 36 | +The number of ways to create the suffix is Choose(Suffix, F - f) x 2^{F - f} x 8^{Suffix - (F - f)} |
| 37 | + |
| 38 | +----- |
| 39 | + |
| 40 | +for(int i = 0; i < N.size(); i++) |
| 41 | + { |
| 42 | + int remaining_digits = (N.size() - 1) - i; |
| 43 | + for(int current_digit = 0; current_digit < N[i] - '0'; current_digit++) |
| 44 | + { |
| 45 | + if(i == 0 && current_digit == 0) |
| 46 | + { |
| 47 | + continue; |
| 48 | + } |
| 49 | + for(auto lucky_frequency : lucky) |
| 50 | + { |
| 51 | + int lucky_prefix_frequency = |
| 52 | + prefix_frequency[4] + prefix_frequency[7] |
| 53 | + + (current_digit == 4 || current_digit == 7); |
| 54 | + |
| 55 | + if(lucky_frequency < lucky_prefix_frequency || |
| 56 | + lucky_frequency > lucky_prefix_frequency + remaining_digits) |
| 57 | + { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + int frequency_4 = prefix_frequency[4] + (current_digit == 4); |
| 62 | + int frequency_7 = prefix_frequency[7] + (current_digit == 7); |
| 63 | + |
| 64 | + int lucky_suffix = lucky_frequency - frequency_4 - frequency_7; |
| 65 | + |
| 66 | + long long choosing_lucky_suffix = choose(remaining_digits, lucky_suffix); |
| 67 | + choosing_lucky_suffix *= power(2, lucky_suffix, MOD); |
| 68 | + choosing_lucky_suffix %= MOD; |
| 69 | + |
| 70 | + int non_lucky_suffix = remaining_digits - lucky_suffix; |
| 71 | + long long choosing_non_lucky_suffix = power(8, non_lucky_suffix, MOD); |
| 72 | + |
| 73 | + long long count_here = choosing_lucky_suffix*choosing_non_lucky_suffix; |
| 74 | + count_here %= MOD; |
| 75 | + |
| 76 | + lucky_count += count_here; |
| 77 | + lucky_count %= MOD; |
| 78 | + |
| 79 | + //cout << "N = " << N << " i = " << i << " d = " << current_digit << " Lucky " << lucky_frequency << "\nLucky Count = " << choosing_lucky_suffix << " others = " << choosing_non_lucky_suffix << " count = " << count_here << "\n"; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + prefix_frequency[N[i] - '0']++; |
| 84 | + } |
| 85 | + |
| 86 | +----- |
| 87 | + |
| 88 | +The number of integers with leading 0s is similar except we do not have to match any prefix. |
| 89 | + |
| 90 | +Just count free suffixes and make sure the final length is strictly smaller than the length of N |
| 91 | + |
| 92 | +----- |
| 93 | + |
| 94 | +for(int i = 1; i < N.size(); i++) |
| 95 | + { |
| 96 | + for(int d = 1; d <= 9; d++) |
| 97 | + { |
| 98 | + int remaining_digits = (N.size() - 1) - i; |
| 99 | + for(auto lucky_frequency : lucky) |
| 100 | + { |
| 101 | + int lucky_here = (d == 4 || d == 7); |
| 102 | + if(lucky_frequency > lucky_here + remaining_digits) |
| 103 | + { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + int lucky_suffix_leading_0 = lucky_frequency - (d == 4 || d == 7); |
| 108 | + |
| 109 | + long long choosing_lucky_suffix_leading_0 = choose(remaining_digits, lucky_suffix_leading_0); |
| 110 | + choosing_lucky_suffix_leading_0 *= power(2, lucky_suffix_leading_0, MOD); |
| 111 | + choosing_lucky_suffix_leading_0 %= MOD; |
| 112 | + |
| 113 | + int non_lucky_suffix_leading_0 = remaining_digits - lucky_suffix_leading_0; |
| 114 | + long long choosing_non_lucky_suffix_leading_0 = power(8, non_lucky_suffix_leading_0, MOD); |
| 115 | + |
| 116 | + long long count_here_leading_0 = choosing_lucky_suffix_leading_0*choosing_non_lucky_suffix_leading_0; |
| 117 | + count_here_leading_0 %= MOD; |
| 118 | + |
| 119 | + lucky_count += count_here_leading_0; |
| 120 | + lucky_count %= MOD; |
| 121 | + |
| 122 | + //cout << "N0 = " << N << " i = " << i << " d = " << d << " Lucky " << lucky_frequency << "\nLucky Count = " << choosing_lucky_suffix_leading_0 << " others = " << choosing_non_lucky_suffix_leading_0 << " count = " << count_here_leading_0 << "\n"; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
0 commit comments