-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-the-largest-palindrome-divisible-by-k.cpp
76 lines (72 loc) · 2.38 KB
/
find-the-largest-palindrome-divisible-by-k.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
// Time: O(n)
// Space: O(1)
// constructive algorithms, math
class Solution {
public:
string largestPalindrome(int n, int k) {
const auto& powmod = [](uint32_t a, uint32_t b, uint32_t mod) {
a %= mod;
uint64_t result = 1;
while (b) {
if (b & 1) {
result = result * a % mod;
}
a = uint64_t(a) * a % mod;
b >>= 1;
}
return result;
};
const auto& inv = [&](int x, int p) {
return powmod(x, p - 2, p);
};
const auto& f = [&](int l) {
static const int p = 7;
string result(l, '9');
if (l == 0) {
return result;
}
int curr = 0;
for (const auto& x : result) {
curr = (curr * 10 + (x - '0')) % p;
}
// l%2 == 0: (curr+(i-9)*11*pow(10, l//2-1, p))%p = 0
// l%2 == 1: (curr+(i-9)*pow(10, l//2, p))%p = 0
int i = 9 - (curr * inv(l % 2 == 0 ? 11 : 1, p) * inv(powmod(10, l / 2 - (l % 2 == 0 ? 1 : 0), p), p)) % p;
if (i <= 2) {
i += p;
}
result[l / 2] = result[l / 2 - (l % 2 == 0 ? 1 : 0)] = '0' + i;
return result;
};
if (k == 1 || k == 3 || k == 9) {
return string(n, '9');
}
if (k == 2 || k == 4 || k == 8) {
k = min(k, 6);
if (n <= k) {
return string(n, '8');
}
const int l = k / 2;
return string(l, '8') + string(n - k, '9') + string(l, '8');
}
if (k == 5) {
if (n <= 2) {
return string(n, '5');
}
return string(1, '5') + string(n - 2, '9') + string(1, '5');
}
if (k == 6) {
if (n <= 2) {
return string(n, '6');
}
if (n % 2) {
const int l = n / 2 - 1;
return string(1, '8') + string(l, '9') + string(1, '8') + string(l, '9') + string(1, '8');
}
const int l = n / 2 - 2;
return string(1, '8') + string(l, '9') + string(2, '7') + string(l, '9') + string(1, '8');
}
const int l = n / 12, r = n % 12;
return string(l * 6, '9') + f(r) + string(l * 6, '9'); // 999999%7 = 0
}
};