首页 > 其他 > 详细

poj 2100 Graveyard Design(尺取法)

时间:2015-08-28 19:21:45      阅读:197      评论:0      收藏:0      [点我收藏+]

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 lines 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<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<algorithm>
 6 using namespace std;
 7 #define ll long long
 8 vector< pair<ll,ll> >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<m;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

 

poj 2100 Graveyard Design(尺取法)

原文:http://www.cnblogs.com/UniqueColor/p/4767568.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!