-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12_Find_the_Count_of_Good_Integers.cpp
131 lines (99 loc) · 3.61 KB
/
12_Find_the_Count_of_Good_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 3272. Find the Count of Good Integers
// You are given two positive integers n and k.
// An integer x is called k-palindromic if:
// x is a palindrome.
// x is divisible by k.
// An integer is called good if its digits can be rearranged to form a k-palindromic integer. For example, for k = 2, 2020 can be rearranged to form the k-palindromic integer 2002, whereas 1010 cannot be rearranged to form a k-palindromic integer.
// Return the count of good integers containing n digits.
// Note that any integer must not have leading zeros, neither before nor after rearrangement. For example, 1010 cannot be rearranged to form 101.
// Example 1:
// Input: n = 3, k = 5
// Output: 27
// Explanation:
// Some of the good integers are:
// 551 because it can be rearranged to form 515.
// 525 because it is already k-palindromic.
// Example 2:
// Input: n = 1, k = 4
// Output: 2
// Explanation:
// The two good integers are 4 and 8.
// Example 3:
// Input: n = 5, k = 6
// Output: 2468
// Constraints:
// 1 <= n <= 10
// 1 <= k <= 9
class Solution
{
public:
long long countGoodIntegers(int n, int k)
{
static long long comb[11][11];
for (int i = 0; i <= 10; i++)
{
comb[i][0] = comb[i][i] = 1;
}
for (int i = 2; i <= 10; i++)
{
for (int j = 1; j < i; j++)
{
comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
}
}
int base = 1;
for (int i = 0; i < (n + 1) / 2; i++)
base *= 10;
vector<long long> encodedFreqs;
for (int half = base / 10; half < base; half++)
{
long long num = half;
for (int j = 0, mirror = ((n & 1) ? half / 10 : half); j < n / 2; j++)
num = num * 10 + (mirror % 10), mirror /= 10;
if (num % k == 0)
{
int freq[10] = {};
for (int t = 0; t < n; t++)
freq[num % 10]++, num /= 10;
num = 0;
for (int d = 0; d < 10; d++)
num = num * 11 + freq[d];
encodedFreqs.push_back(num);
}
}
sort(encodedFreqs.begin(), encodedFreqs.end());
encodedFreqs.erase(unique(encodedFreqs.begin(), encodedFreqs.end()), encodedFreqs.end());
long long total = 0;
for (long long code : encodedFreqs)
{
int freq[10] = {};
for (int d = 9; d >= 0; d--)
freq[d] = code % 11, code /= 11;
long long ways = 1;
for (int i = 0, rem = n; i < 10; i++)
ways *= comb[i ? rem : rem - 1][freq[i]], rem -= freq[i];
total += ways;
}
return total;
}
};
/*
Code Explanation:
This solution finds the count of good integers with n digits that can be rearranged to form k-palindromic numbers.
1. First, it precomputes combinations using Pascal's triangle (comb array)
2. Generates half of palindrome numbers and extends them to full palindromes
3. For each valid palindrome (divisible by k), it:
- Counts frequency of each digit
- Encodes these frequencies into a single number
- Stores unique frequency patterns
4. Finally, for each unique frequency pattern:
- Decodes back the frequencies
- Calculates number of possible arrangements using combinations
- Adds to total count
Time Complexity: O(10^((n+1)/2) * n)
- Main work is in generating half palindromes which is O(10^((n+1)/2))
- For each palindrome, we do O(n) work
Space Complexity: O(10^((n+1)/2))
- For storing encoded frequencies
- Additional O(1) space for combinations array (11x11)
*/