-
Notifications
You must be signed in to change notification settings - Fork 111
/
sum-of-k-mirror-numbers.cc
59 lines (56 loc) · 1.21 KB
/
sum-of-k-mirror-numbers.cc
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
// Sum of k-Mirror Numbers
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
class Solution {
public:
long long kMirror(int k, int n) {
long ret = 0, start = 1, stop = 10;
for (long d = 1; n; d++) {
FOR(i, start, stop) {
long x = i, y = d%2 ? i/10 : i;
for (; y; y /= 10)
x = x*10+y%10;
y = 0;
for (long t = x; t; t /= k)
y = y*k+t%k;
if (x == y) {
ret += x;
if (--n == 0) break;
}
}
if (d%2 == 0) {
start = stop;
stop *= 10;
}
}
return ret;
}
};
///
class Solution {
long get(long x, long y) {
for (; y; y /= 10)
x = x*10+y%10;
return x;
}
bool is(long x, int k) {
long y = 0;
for (long t = x; t; t /= k)
y = y*k+t%k;
return x == y;
}
public:
long long kMirror(int k, int n) {
long ret = 0, odd = 1, even = 1, oddPal, evenPal;
while (n--) {
while (oddPal = get(odd, odd/10), !is(oddPal, k))
odd++;
while (evenPal = get(even, even), !is(evenPal, k) && oddPal > evenPal)
even++;
if (oddPal < evenPal)
ret += oddPal, odd++;
else
ret += evenPal, even++;
}
return ret;
}
};