题面:
Graveyard Design
Time Limit: 10000MS
Memory Limit: 64000K
Total Submissions: 9489
Accepted: 2401
Case Time Limit: 2000MS
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.
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
题面描述:
要求墓地由s*s个墓碑组成,找到k个连续的s使得这k个墓地的墓碑总数为n,这样的组合可能有多种,输出全部的可能并按k的个数降序排列。
题目分析:
基础的尺取题,双指针从1开始,前指针每到一个数sum就加上它的平方,当sum的值大于所求的值时后指针开始向前,每到一个数sum就减去它的平方,当sum等于所求值时保存一下前后指针。接着往下找直到结束。最后按保存好的前后指针的差值排序一下,从大到小输出就行。
代码:
#include<algorithm>
#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
long long n,sum=0,i2=0,i3=0;//这里sum和i2,i3如果不用longlong后面数很大到1e14就判断不了了
int num1=0,num3=0,num2[1000]={0},i1[1000]={0},t1,t2;
scanf("%lld",&n);
while(i2<10000005){
i2++,sum+=i2*i2,num3++;
while(sum>n) i3++,sum-=i3*i3,num3--;
if(sum==n) i1[num1]=i3+1,num2[num1]=num3,num1++;//这里不用i2++
}
printf("%d",num1);
for(i2=0;i2<num1;i2++){
printf("\n%d",num2[i2]);
for(i3=0;i3<num2[i2];i3++) printf(" %d",i1[i2]++);
}
}