POJ_2100_Graveyard_Design_(尺取法)

描述


http://poj.org/problem?id=2100

求连续平方和=n的序列个数,并输出序列.

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

分析


直接尺取.

注意:

1.如果要用开根计算的话要写成 " ll ub=(ll)sqrt(n*1.0); "写成 " ll ub=sqrt(n); "会CE.

所以干脆写成 " r*r<=n "

 #include<cstdio>
#include<queue>
#define ll long long
using std :: queue; struct node
{
ll len,fst;
node() {}
node(ll a,ll b) : len(a),fst(b) {}
};
queue <node> q;
ll n; inline ll val(ll x) { return x*x; } int main()
{
#ifndef ONLINE_JUDGE
freopen("grave.in","r",stdin);
freopen("grave.out","w",stdout);
#endif
scanf("%lld",&n);
ll l=,r=,k=,len=;
ll sum=;
while(val(r)<=n)
{
if(sum<n)
{
sum+=val(++r);
len++;
}
else if(sum>n)
{
sum-=val(l++);
len--;
}
else
{
q.push(node(len,l));
k++;
sum-=val(l++);
len--;
}
}
printf("%lld",k);
while(!q.empty())
{
node t=q.front(); q.pop();
ll len=t.len,fst=t.fst;
printf("\n%lld ",len);
for(ll i=;i<len;i++) printf("%lld ",fst+i);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
上一篇:PHP时间格式控制符对照表


下一篇:质量基础设施一站式线上服务平台建设,NQI系统开发