poj 2100 Graveyard Design(尺取法)

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 s2graves. 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 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 using namespace std;
 7 #define ll long long
 8 vector< pair >ans;
 9 
10 int main()
11 {
12     ll n;
13     while(scanf("%I64d",&n)==1){
14         ll s=1,t=1;
15         ll sum=0;
16         for(;;){
17 
18             while(sum<n){
19                 sum=sum+t*t;
20                 t++;
21             }
22             if((t-1)*(t-1)>n)
23                 break;
24             if(sum==n){
25                 ans.push_back(make_pair(s,t-1));
26             }
27             sum-=s*s;
28             s++;
29 
30         }
31         ll m=ans.size();
32         printf("%I64d\n",m);
33         for(int i=0;i){
34             ll l=ans[i].first;
35             ll r=ans[i].second;
36             printf("%I64d",r-l+1);
37             for(ll j=l;j<=r;j++){
38                 printf(" %I64d",j);
39             }
40             printf("\n");
41 
42         }
43     }
44     return 0;
45 }
View Code

 

转载于:https://www.cnblogs.com/UniqueColor/p/4767568.html

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