Graveyard Design
Time Limit: 10000MS | Memory Limit: 64000K | |
Total Submissions: 6107 | Accepted: 1444 | |
Case Time Limit: 2000MS |
Description
King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves.
After a consultation with his astrologer, King George decided that the lengths of section sides must be a sequence of successive positive integer numbers. A section with side length s contains s 2 graves. George has estimated the total number of graves that will be located on the graveyard and now wants to know all possible graveyard designs satisfying the condition. You were asked to find them.
After a consultation with his astrologer, King George decided that the lengths of section sides must be a sequence of successive positive integer numbers. A section with side length s contains s 2 graves. George has estimated the total number of graves that will be located on the graveyard and now wants to know all possible graveyard designs satisfying the condition. You were asked to find them.
Input
Input file contains n --- the number of graves to be located in the graveyard (1 <= n <= 10
14 ).
Output
On the first line of the output file print k --- the number of possible graveyard designs. Next k lines must contain the descriptions of the graveyards. Each line must start with l --- the number of sections in the corresponding graveyard, followed by l integers --- the lengths of section sides (successive positive integer numbers). Output line's in descending order of l.
Sample Input
2030
Sample Output
2 4 21 22 23 24 3 25 26 27题意:给一个数n,求有多少个连续的数的平方和为n并输出
思路:因为平方数具有单调性,我们可以用尺取法推进,然后用一个数组保存下来即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define LL long long
int b[1000100][2];
int cnt;
int main()
{
LL n;
while(~scanf("%lld",&n))
{
LL l=1,r=1,num=0;
LL ans=0;
LL t=(int)sqrt(n*1.0);
while(l<=t)
{
while(r<=t&&ans<n)
{
ans+=r*r;
r++;
}
if(ans==n)
{
b[num][0]=l;
b[num][1]=r;
num++;
}
ans-=l*l;
l++;
}
printf("%d\n",num);
for(int i=0;i<num;i++)
{
printf("%d ",b[i][1]-b[i][0]);
for(int j=b[i][0];j<b[i][1]-1;j++)
printf("%d ",j);
printf("%d\n",b[i][1]-1);
}
}
return 0;
}