poj 2100(尺取法)
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 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.
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
题意:给你一个数,询问有多少种连续自然数的平方和等于这个数,输出所有可能
题解:尺取法遍历所有符合条件的区间,满足的话记录左边界以及右边界,计数器+1。
尺取法过程:
整个过程分为4布:
1.初始化左右端点
2.不断扩大右端点,直到满足条件
3.如果第二步中无法满足条件,则终止,否则更新结果
4.将左端点扩大1,然后回到第二步
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> using namespace std; typedef long long LL; LL n; struct Node{ LL l,r; }res[1000007]; int main() { while(scanf("%lld",&n)!=EOF){ LL l=1,r=1; LL len = (int)sqrt(n*1.0)+1; LL sum = 0; int cnt=0; while(l<=len){ while(r<=len&&sum<n){ sum+=r*r; r++; } if(sum<n) break; if(sum==n){ cnt++; res[cnt].l = l; res[cnt].r = r; } sum-=l*l; l++; } printf("%d\n",cnt); for(int i=1;i<=cnt;i++){ printf("%d ",res[i].r-res[i].l); for(int j=res[i].l;j<res[i].r-1;j++){ printf("%d ",j); } printf("%d\n",res[i].r-1); } } return 0; }