forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum-of-k-mirror-numbers.cpp
97 lines (90 loc) · 2.49 KB
/
sum-of-k-mirror-numbers.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
// Time: O(10^6), the most times of finding x is 665502 (k = 7, n = 30)
// Space: O(1)
class Solution {
public:
long long kMirror(int k, int n) {
const int base1 = k, base2 = 10; // (10, k) is slower
int64_t result = 0;
vector<int> prefix_num(2, 1), total(2, base1);
uint8_t odd = 1;
while (n--) {
int64_t x;
do {
x = mirror(prefix_num[odd], base1, odd);
if (++prefix_num[odd] == total[odd]) {
total[odd] *= base1;
odd ^= 1;
}
} while (x != reverse(x, base2));
result += x;
}
return result;
}
private:
int64_t mirror(int n, int base, bool odd) {
int64_t result = n;
if (odd) {
n /= base;
}
for (; n; n /= base) {
result = result * base + (n % base);
}
return result;
}
int64_t reverse(int64_t n, int base) {
int64_t result = 0;
for (; n; n /= base) {
result = result * base + n % base;
}
return result;
}
};
// Time: O(10^6), the most times of finding x is 665502 (k = 7, n = 30)
// Space: O(1)
class Solution2 {
public:
long long kMirror(int k, int n) {
string s = "0";
int64_t result = 0;
while (n--) {
int64_t x;
do {
x = next_num_in_base_k(k, &s);
} while (!is_mirror(to_string(x)));
result += x;
}
return result;
}
private:
int64_t next_num_in_base_k(int k, string *s) {
int result = 0;
for (int i = size(*s) / 2; i < size(*s); ++i) {
if ((*s)[i] + 1 - k < '0') {
(*s)[i] = (*s)[size(*s) - 1 - i] = (*s)[i] + 1;
break;
}
(*s)[i] = (*s)[size(*s) - 1 - i] = '0';
}
if ((*s)[0] == '0') {
s->back() = '1';
s->insert(begin(*s), '1');
}
return to_int_from_base_k(*s, k);
}
int64_t to_int_from_base_k(const string& s, int k) {
int64_t result = 0;
for (int64_t i = size(s) - 1, base = 1; i >= 0; --i, base *= k) {
result += (s[i] - '0') * base;
}
return result;
}
bool is_mirror(const string& s) {
int left = 0, right = size(s) - 1;
while (left < right) {
if (s[left++] != s[right--]) {
return false;
}
}
return true;
}
};