这幅图便是尺取法怎么“取”的过程了。
整个过程分为4布:
1.初始化左右端点
2.不断扩大右端点,直到满足条件
3.如果第二步中无法满足条件,则终止,否则更新结果
4.将左端点扩大1,然后回到第二步
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define LL long long
using namespace std;
vector<pair<LL, LL> > ans;
void solve(LL n)
{
ans.clear();
LL s = 1, t = 1;
LL sum = 0;
while (s * s <= n)
{
while (t * t <= n && sum < n)
{
sum += t * t;
t++;
}
if (sum == n)
{
ans.push_back(make_pair(s, t));
}
sum -= s * s;
s++;
}
int sz = ans.size();
printf("%d\n", sz);
for (int i = 0; i < sz; i++)
{
printf("%d", ans[i].second - ans[i].first);
for (int j = ans[i].first; j < ans[i].second; j++)
{
printf(" %d", j);
}
puts("");
}
}
int main()
{
LL n;
while (~scanf("%lld", &n))
{
solve(n);
}
return 0;
}