-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2081.cpp
More file actions
59 lines (59 loc) · 1.54 KB
/
2081.cpp
File metadata and controls
59 lines (59 loc) · 1.54 KB
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
class Solution {
public:
int vec[100];
long long kMirror(int k, int n) {
int length = 1;
long long sum = 0;
int cnt = 0;
while (true) {
int low = pow(10, length - 1);
int high = pow(10, length);
for (int i = low; i < high; ++i) {
long long mirror = make(i, 0);
if (check(mirror, k)) {
sum += mirror;
cnt++;
}
if (cnt == n) return sum;
}
for (int i = low; i < high; ++i) {
long long mirror = make(i, 1);
if (check(mirror, k)) {
sum += mirror;
cnt++;
}
if (cnt == n) return sum;
}
length++;
}
return -1;
}
long long make(long long num, bool flag) {
long long temp = num;
long long appendix = 0;
int cnt = 0;
while (temp) {
appendix = appendix * 10 + temp % 10;
temp /= 10;
cnt++;
}
if (!flag) num /= 10;
while (cnt--) num *= 10;
return num + appendix;
}
bool check(long long num, int k) {
int t = 0;
while (num) {
vec[t++] = num % k;
num /= k;
}
int left = 0;
int right = t - 1;
while (left < right) {
if (vec[left] != vec[right]) return false;
left++;
right--;
}
return true;
}
};