Graveyard Design
Time Limit: 10000MS | Memory Limit: 64000K | |
Total Submissions: 12120 | Accepted: 3055 | |
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 s2 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 <= 1014 ).
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
Source
Northeastern Europe 2004, Northern Subregion
题意是给你一个数,你要找出一段连续的序列,使得这段序列平方的和等于这个数,
输出所有的结果,并且按照长度降序输出每段序列。
因为是一个区间嘛,所以想到尺取法。
代码我是看的别人的,如果要我来写的话,我一般是不会用vector这种的,最多用用map。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define ll long long
ll n;
typedef pair<int,int> P;
vector<P> p;
int main(){
cin >> n;
ll l = 1,r = 1;
ll sum = 0;
while(l * l <= n){
if(sum == n) p.push_back(P(r-l, l));
if(sum <= n) sum += r * r, r++;
else sum -= l*l, l++;
}
int cnt = p.size();
sort(p.begin(),p.end());
cout << cnt << endl;
for(int i= cnt-1; i>= 0; i--){
printf("%d", p[i].first);
for(int j= p[i].second; j< p[i].second + p[i].first; j++)
printf(" %d", j);
printf("\n");
}
return 0;
}