poj 2100 Graveyard Design (two pointers ,尺取法)

Graveyard Design
Time Limit: 10000MS   Memory Limit: 64000K
Total Submissions: 5567   Accepted: 1289
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 /*************************************************************************
 2     > File Name: code/poj/2100.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年09月25日 星期五 00时42分49秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21 #define y1 hust111qqz
22 #define yn hez111qqz
23 #define j1 cute111qqz
24 #define ms(a,x) memset(a,x,sizeof(a))
25 #define lr dying111qqz
26 using namespace std;
27 #define For(i, n) for (int i=0;i<int(n);++i)  
28 typedef long long LL;
29 typedef double DB;
30 const int inf = 0x3f3f3f3f;
31 LL n;
32 LL maxn;
33 vector<LL>ans;
34 
35 
36 void solve()
37 {
38     LL head = 1,tail = 1;
39     LL sum = 0 ;
40 
41     while (tail<=maxn)
42     {
43     sum = sum + tail*tail;
44 
45     if (sum>=n)
46     {
47         while (sum>n)//主要是while,因为可能要减掉多个才能小于n
48         {
49         sum -= head*head;
50         head++;
51         }
52         if (sum==n)
53         {//因为要先输出答案个数...所以必须存起来延迟输出...
54         ans.push_back(head);
55         ans.push_back(tail);
56         }
57     }
58     tail++;
59     }
60     LL sz = ans.size();
61     printf("%lld\n",sz/2);
62     for (LL i = 0 ; i<ans.size() ; i = i +2)
63     {
64     printf("%lld",ans[i+1]-ans[i]+1);
65     for ( LL j = ans[i] ; j <=ans[i+1] ; j++) printf(" %lld",j);
66     printf("\n");
67     }
68     
69 }
70 int main()
71 {
72   #ifndef  ONLINE_JUDGE 
73    freopen("in.txt","r",stdin);
74   #endif
75     scanf("%lld",&n);  
76     maxn = ceil(sqrt(n));
77     solve();
78    
79  #ifndef ONLINE_JUDGE  
80   fclose(stdin);
81   #endif
82     return 0;
83 }
View Code

 

posted @ 2015-09-25 01:29  111qqz  阅读(146)  评论(0编辑  收藏  举报