-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_2081_kMirror.cc
More file actions
52 lines (49 loc) · 1.54 KB
/
Copy pathProblem_2081_kMirror.cc
File metadata and controls
52 lines (49 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
#include <vector>
using namespace std;
class Solution {
public:
long long kMirror(int k, int n) {
auto isPalindrome = [&](long long x) {
int digit[100] = {0};
int length = -1;
while (x) {
++length;
digit[length] = x % k;
x /= k;
}
for (int i = 0, j = length; i < j; ++i, --j) {
if (digit[i] != digit[j]) {
return false;
}
}
return true;
};
int left = 1;
int count = 0;
long long ans = 0;
while (count < n) {
// 折半搜索,分左右两个部分数字
int right = left * 10;
// 0表示枚举奇数长度回文,1表示枚举偶数长度回文
for (int op = 0; op < 2; op++) {
// 枚举数字
for (int i = left; i < right && count < n; i++) {
long long num = i;
int x = op == 0 ? (num / 10) : num;
// 拼接数字
while (x != 0) {
num = num * 10 + x % 10;
x /= 10;
}
// 这样拼接出来的数字一定是10进制下对称,再检验是否k进制下对称
if (isPalindrome(num)) {
count++;
ans += num;
}
}
}
left = right;
}
return ans;
}
};