Trend MicroOA #3
Find Sequences
Estimated MediumJudge-ready
Topics (AI-labelled)Two PointersBits & Math
Given a positive integer n, find all non-empty sequences of consecutive positive integers p, p+1, …, p+c−1 whose c squares sum to n. p≥1 and c≥1, and single-term sequences must be output as well. Output all sequences in decreasing order of their number of terms c. There is at most one sequence of each length, so this order is unique. The fixed original body text fully specifies 1≤n≤10^10; this site restores from the original HTML the rules and range that the compiled version's parser swallowed, and converts the original returned string array into line-by-line output.
Input
One line with a positive integer n, 1≤n≤10000000000.
Output
The first line contains the total number of sequences k. Each of the next k lines first gives the sequence's number of terms c, then all c of its consecutive positive integers, separated by spaces. Output the lines in decreasing order of c. If there is no solution, output only one line containing 0. Output the elements themselves, not their squares.
Example 1
Input:
1
Output:
1
1 1
Example 2
Input:
2
Output:
0
Example 3
Input:
25
Output:
2
2 3 4
1 5
Example notes
For n=1 there is only [1]; n=2 has no solution; n=25 has 3²+4²=25 and 5²=25, so the longer [3,4] is output before [5].
Editorial
Loading solution…