-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2698.cpp
More file actions
37 lines (33 loc) · 964 Bytes
/
2698.cpp
File metadata and controls
37 lines (33 loc) · 964 Bytes
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
// Daily 15.2.2025
// 2698. Find the Punishment Number of an Integer
// https://leetcode.com/problems/find-the-punishment-number-of-an-integer/submissions/1544402294
struct CompileTimeCalc {
static consteval bool part(int x, int goal) {
constexpr int m[] = {10, 100, 1000};
if(goal < 0 || x < goal) return false;
if(x == goal) return true;
return part(x / m[0], goal - x % m[0]) ||
part(x / m[1], goal - x % m[1]) ||
part(x / m[2], goal - x % m[2]);
}
static consteval std::array<int, 1001> getArr()
{
std::array<int, 1001> a;
a[0] = 0;
for (auto i = 1; i < 1001; i++)
{
a[i] = a[i-1];
if(part(i*i, i))
a[i] += i*i;
}
return a;
}
};
class Solution {
public:
int punishmentNumber(int n) {
return ar[n];
}
private:
static const constexpr auto ar = CompileTimeCalc::getArr();
};