poj2100
题目给出一个数,求一个连续序列使得它们的平方和等于这个数
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define pii pair<int,int>
#define ll long long
vector<pii>v;
bool cmp(pii a,pii b)
{
return (a.second-a.first+1)>(b.second-b.first+1);
}
int main()
{
v.clear();
ll n,low=1,up,sum=0;
cin>>n;
for(up=1;up*up<=n;up++)
{
sum+=up*up;
if(sum==n)
{
v.push_back(make_pair(low,up));
}
while(low<up&&sum>=n)
{
sum-=low*low;
low++;
if(sum==n)
{
v.push_back(make_pair(low,up));
}
}
}
sort(v.begin(),v.end(),cmp);
int sz=v.size();
cout<<sz<<endl;
for(int i=0;i<sz;i++)
{
cout<<v[i].second-v[i].first+1<<" ";
for(int j=v[i].first;j<=v[i].second;j++)
cout<<j<<" ";
cout<<endl;
}
return 0;
}