POJ 2100 Graveyard Design(尺取法)

Graveyard Design
Time Limit: 10000MS   Memory Limit: 64000K
Total Submissions: 5844   Accepted: 1376
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

题意:查找出一段连续的数的平方和等于n,问有几组方案,并输出方案。


题解:n超出了int,SB的我居然想到了给所有数的平方打表,这几天打表中毒不浅~~~比较简单,直接尺取法查找就可以了。时间复杂度为O(n)。


代码如下:


#include<cstdio>
#include<cstring>
#include<cmath>
#define LL __int64
int num[2000],left[2000],right[2000];

void solve(LL n)
{
	int i,j;
	LL m=(LL)sqrt(n*1.0);
	LL sum=0,l=1,r=0;
	int cnt=0;
	while(1)
	{
		while(sum<n)
		{
			r++;
			sum+=r*r;
		}
		if(r>m)
			break;
		if(sum==n)
		{
			num[cnt]=r-l+1;
			left[cnt]=l;
			right[cnt]=r;
			cnt++;
		}
		sum-=(l*l);
		l++;
	}
	printf("%d\n",cnt);
	for(i=0;i<cnt;++i)
	{
		printf("%d ",num[i]);
		for(j=left[i];j<right[i];++j)
			printf("%d ",j);
		printf("%d\n",right[i]);
	}
} 

int main()
{
	LL n;
	while(scanf("%I64d",&n)!=EOF)
		solve(n);
	return 0; 
} 



你可能感兴趣的:(POJ 2100 Graveyard Design(尺取法))