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
  • 相关阅读:
    卡顿检测
    FPS检测
    当检测到慢函数时,会在后台线程完成慢函数的分析
    慢函数检测
    对于没有复现onWindowFocusChange方法的Activity子类,插入一个onWindowFocusChange方法
    将totalCost用insurance的set方法set到保险总费用的字段中,然后进行插入操作。代码如下
    XML基础学习02<linq to xml>
    XML基础学习01
    Ajax学习记录
    数据迁移
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4767568.html
Copyright © 2011-2022 走看看