POJ-2100
题目正文
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² 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
解析:其实题目就是告诉你说当输入一个小于^14字时候,要找出所有连续子序列其中每个数字的平方之和要等于输入的数字,而且还要输出这段序列的长度(从大到小输出)。
#include<stdio.h>
#include<math.h>
int b[1000000][2];
int main()
{ long long n;
long long sum,t,flag;
long long start,end; //从小到大,strat在左,end在右
long long i,j,s;
while(~scanf("%lld",&n))
{
t=sum=0;
start=end=1;
s=(long long)sqrt(n*1.0); //由于是平方和等于n,所以最大只需要找到sqrt(n*1.0)
while(start<=s)
{
while(end<=s&&sum<n) //右指标向右扩展,并且sum<n的情况下
{
sum+=end*end; //sum加上它的平方和
end++; //右指标再向右
}
//跳出循环的时候sum>=n
if(sum==n) {b[t][0]=start;b[t][1]=end;t++;} //如果sum==n,记录左右指标
sum-=start*start; //继续寻找下一个序列,此时应该让strat向右缩
start++;
}
printf("%lld\n",t); //输出
for(i=0;i<t;i++)
{ flag=1;
printf("%d ",b[i][1]-b[i][0]);
for(j=b[i][0];j<b[i][1];j++)
{
if(flag){printf("%lld",j);flag=0;}
else printf(" %lld",j);
}
printf("\n");
}
}
return 0;
}