zoukankan      html  css  js  c++  java
  • 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<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
    ",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("
    ");
    41 
    42         }
    43     }
    44     return 0;
    45 }
    View Code
  • 相关阅读:
    AtCoder Beginner Contest 113 D Number of Amidakuji
    UVA
    mt19937 -- 高质量随机数
    牛客网NOIP赛前集训营-提高组(第七场)C 洞穴
    牛客OI周赛4-提高组 C 战争(war)
    牛客OI周赛4-提高组 B 最后的晚餐(dinner)
    bzoj 4318 || 洛谷P1654 OSU!
    Tourists Codeforces
    bzoj 1791 [Ioi2008]Island 岛屿
    洛谷 P2231 [HNOI2002]跳蚤
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4767568.html
Copyright © 2011-2022 走看看