-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2698.cpp
27 lines (27 loc) · 883 Bytes
/
2698.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
class Solution {
public:
bool _partitionable(int position, int tempSum, int currentSum, int k, string& kS, int ori) {
if (currentSum + tempSum > ori) return false;
if (position == kS.size()) {
return currentSum == ori;
}
tempSum *= 10;
tempSum += kS[position] - '0';
if (_partitionable(position + 1, tempSum, currentSum, k, kS, ori)) return true;
if (_partitionable(position + 1, 0, currentSum + tempSum, k, kS, ori)) return true;
return false;
}
bool partitionable(int k, int ori) {
string kS = to_string(k);
return _partitionable(0, 0, 0, k, kS, ori);
}
int punishmentNumber(int n) {
int sum = 0;
for (int i = 1; i <= n; ++i) {
if (partitionable(i * i, i)) {
sum += i * i;
}
}
return sum;
}
};