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 "

 

 1 #include<cstdio>
 2 #include<queue>
 3 #define ll long long
 4 using std :: queue;
 5 
 6 struct node 
 7 {
 8     ll len,fst;
 9     node() {}
10     node(ll a,ll b) : len(a),fst(b) {}
11 };
12 queue <node> q;
13 ll n;
14 
15 inline ll val(ll x) { return x*x; }    
16 
17 int main()
18 {
19 #ifndef ONLINE_JUDGE
20     freopen("grave.in","r",stdin);
21     freopen("grave.out","w",stdout);
22 #endif
23     scanf("%lld",&n);
24     ll l=1,r=0,k=0,len=0;
25     ll sum=0;
26     while(val(r)<=n)
27     {
28         if(sum<n)
29         {
30             sum+=val(++r);
31             len++;
32         }
33         else if(sum>n)
34         {
35             sum-=val(l++);
36             len--;
37         }
38         else
39         {
40             q.push(node(len,l));
41             k++;
42             sum-=val(l++);
43             len--;
44         }
45     }
46     printf("%lld",k);
47     while(!q.empty())
48     {
49         node t=q.front(); q.pop();
50         ll len=t.len,fst=t.fst;
51         printf("\n%lld ",len);
52         for(ll i=0;i<len;i++) printf("%lld ",fst+i);
53     }
54 #ifndef ONLINE_JUDGE
55     fclose(stdin);
56     fclose(stdout);
57 #endif
58     return 0;
59 }
View Code

 

 

 

posted @ 2016-04-24 20:14  晴歌。  阅读(155)  评论(0编辑  收藏  举报