zoukankan      html  css  js  c++  java
  • ICPC Yokohama, 2018(Domestic)C. Skyscraper “MinatoHarukas ” (暴力)

    Description

    Mr. Port plans to start a new business renting one or more floors of the new skyscraper with one giga floors, MinatoHarukas. He wants to rent as many vertically adjacent floors as possible, because he wants to show advertisement on as many vertically adjacent windows as possible. The rent for one floor is proportional to the floor number, that is, the rent per month for the n-th floor is n times that of the first floor. Here, the ground floor is called the first floor in the American style, and basement floors are out of consideration for the renting. In order to help Mr. Port, you should write a program that computes the vertically adjacent floors satisfying his requirement and whose total rental cost per month is *exactly equal* to his budget.For example, when his budget is 15 units, with one unit being the rent of the first floor, there are four possible rent plans, 1+2+3+4+5, 4+5+6, 7+8, and 15. For all of them, the sums are equal to 15. Of course in this example the rent of maximal number of the floors is that of 1+2+3+4+5, that is, the rent from the first floor to the fifth floor.

    Input

    The input consists of multiple datasets, each in the following format.

    • b

    A dataset consists of one line, the budget of Mr. Port b as multiples of the rent of the first floor. b is a positive integer satisfying 1 < b < (10^9). The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 1000.

    Output

    For each dataset, output a single line containing two positive integers representing the plan with the maximal number of vertically adjacent floors with its rent price exactly equal to the budget of Mr. Port. The first should be the lowest floor number and the second should be the number of floors.

    样例输入

    15
    16
    2
    3
    9699690
    223092870
    847288609
    900660121
    987698769
    999999999
    0
    

    样例输出

    1 5
    16 1
    2 1
    1 2
    16 4389
    129 20995
    4112949 206
    15006 30011
    46887 17718
    163837 5994
    

    大意就是找自然数列里和为n的最长的一段。设这一段起始的数为a,长度为l,那么和为(a imes l +frac{l imes (l-1)}{2}),注意到l大致在(sqrt{n})范围内(把l换为(sqrt{n})的话离着n已经很接近了),不妨扩大一下(比如(3 imes sqrt{n}))然后从大到小枚举i,找到一组符合的直接break即可。

    记得开long long!

    #include <bits/stdc++.h>
    #define int long long
    using namespace std;
    int n;
    signed main()
    {
        while(scanf("%lld", &n) && n)
        {
            for(int i = sqrt(n) * 3; i >=1; i--)
            {
                int a = (n - i*(i - 1) / 2) / i;
                if(a * i + i * (i - 1) / 2 == n && a > 0)
                {
                    cout << a << ' ' << i << endl;
                    break;
                }
            }
        }
    }
    
  • 相关阅读:
    ReSharper.8.0.14.856注册码
    asp.net 301重定向代码
    WebResource.axd 404 错误
    【原创】asp.net导出word 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a
    C#操作word或excel及水晶报表,检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 80070005
    Repository 设计模式介绍
    将15位身份证转换成18位
    清理数据库木马文件
    Oracle的创建表空间及用户
    使用python脚本执行地理处理工具
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13440497.html
Copyright © 2011-2022 走看看