import java.util.*;
import java.lang.*;
import java.io.*;
class ADG {
int totalPages = 411;
int expectedDays = 6;
for (int firstDay = 1; firstDay <= totalPages; firstDay++) {
Stack<Integer> solution = checkSolution(0, firstDay, totalPages);
if (solution != null && solution.size() == expectedDays) {
printSolution(solution);
}
}
}
private static void printSolution(Stack<Integer> solution) {
int i = 1;
while (!solution.empty()) {
int pages = solution.pop();
System.
out.
println("Day " + i
+ " " + pages
+ " pages"); i++;
}
}
private static Stack<Integer> checkSolution(int soFar, int current, int total) {
int totalRead = soFar + current;
if (totalRead == total) {
// reached the total
Stack<Integer> solution = new Stack<Integer>();
solution.push(current);
return solution;
}
if (totalRead > total) {
// already passed total
return null;
}
// see if this leads to a solution
Stack<Integer> solution = checkSolution(totalRead, squareOfDigitSum(totalRead), total);
if (solution == null) {
// doesn't lead to a solution
return solution;
}
solution.push(current);
return solution;
}
public static int squareOfDigitSum(int pNum) {
int total = 0;
while (pNum > 0) {
total += pNum % 10;
pNum /= 10;
}
return (total * total);
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBBREcgewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24gewogICAgICAgIGludCB0b3RhbFBhZ2VzID0gNDExOwogICAgICAgIGludCBleHBlY3RlZERheXMgPSA2OwogICAgICAgIGZvciAoaW50IGZpcnN0RGF5ID0gMTsgZmlyc3REYXkgPD0gdG90YWxQYWdlczsgZmlyc3REYXkrKykgewogICAgICAgICAgICBTdGFjazxJbnRlZ2VyPiBzb2x1dGlvbiA9IGNoZWNrU29sdXRpb24oMCwgZmlyc3REYXksIHRvdGFsUGFnZXMpOwogICAgICAgICAgICBpZiAoc29sdXRpb24gIT0gbnVsbCAmJiBzb2x1dGlvbi5zaXplKCkgPT0gZXhwZWN0ZWREYXlzKSB7CiAgICAgICAgICAgICAgICBwcmludFNvbHV0aW9uKHNvbHV0aW9uKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAKICAgIHByaXZhdGUgc3RhdGljIHZvaWQgcHJpbnRTb2x1dGlvbihTdGFjazxJbnRlZ2VyPiBzb2x1dGlvbikgewogICAgICAgIGludCBpID0gMTsKICAgICAgICB3aGlsZSAoIXNvbHV0aW9uLmVtcHR5KCkpIHsKICAgICAgICAgICAgaW50IHBhZ2VzID0gc29sdXRpb24ucG9wKCk7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRGF5ICIgKyBpICsgIiAiICsgcGFnZXMgKyAiIHBhZ2VzIik7CiAgICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICB9CiAgCiAgICBwcml2YXRlIHN0YXRpYyBTdGFjazxJbnRlZ2VyPiBjaGVja1NvbHV0aW9uKGludCBzb0ZhciwgaW50IGN1cnJlbnQsIGludCB0b3RhbCkgewogICAgCWludCB0b3RhbFJlYWQgPSBzb0ZhciArIGN1cnJlbnQ7CiAgICAJCiAgICAgICAgaWYgKHRvdGFsUmVhZCA9PSB0b3RhbCkgewogICAgICAgIAkvLyByZWFjaGVkIHRoZSB0b3RhbAogICAgICAgICAgICBTdGFjazxJbnRlZ2VyPiBzb2x1dGlvbiA9IG5ldyBTdGFjazxJbnRlZ2VyPigpOwogICAgICAgICAgICBzb2x1dGlvbi5wdXNoKGN1cnJlbnQpOwogICAgICAgICAgICByZXR1cm4gc29sdXRpb247CiAgICAgICAgfQogICAgICAgIGlmICh0b3RhbFJlYWQgPiB0b3RhbCkgewogICAgICAgICAgICAvLyBhbHJlYWR5IHBhc3NlZCB0b3RhbAogICAgICAgICAgICByZXR1cm4gbnVsbDsKICAgICAgICB9IAoKICAgICAgICAvLyBzZWUgaWYgdGhpcyBsZWFkcyB0byBhIHNvbHV0aW9uCiAgICAgICAgU3RhY2s8SW50ZWdlcj4gc29sdXRpb24gPSBjaGVja1NvbHV0aW9uKHRvdGFsUmVhZCwgc3F1YXJlT2ZEaWdpdFN1bSh0b3RhbFJlYWQpLCB0b3RhbCk7CgogICAgICAgIGlmIChzb2x1dGlvbiA9PSBudWxsKSB7CiAgICAgICAgCS8vIGRvZXNuJ3QgbGVhZCB0byBhIHNvbHV0aW9uCiAgICAgICAgICAgIHJldHVybiBzb2x1dGlvbjsKICAgICAgICB9CgogICAgICAgIHNvbHV0aW9uLnB1c2goY3VycmVudCk7CiAgICAgICAgcmV0dXJuIHNvbHV0aW9uOwogICAgfQogIAogICAgcHVibGljIHN0YXRpYyBpbnQgc3F1YXJlT2ZEaWdpdFN1bShpbnQgcE51bSkgewogICAgICAgIGludCB0b3RhbCA9IDA7CiAgICAgICAgd2hpbGUgKHBOdW0gPiAwKSB7CiAgICAgICAgICAgIHRvdGFsICs9IHBOdW0gJSAxMDsKICAgICAgICAgICAgcE51bSAvPSAxMDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuICh0b3RhbCAqIHRvdGFsKTsKICAgIH0KCn0=