题目描述

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.

输入

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

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 < 109.

The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 1000.

输出

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

原来是一个等差数列。把求和公式化简为是一个一元二次方程在利用韦达定理求解就行了。

(韦达定理:x1+x2= -(b/a)    ;    x1*x2=c/a);

直接求是会超时的。一层循环是1e9,只要有另一层循环,不管是多小,一般是会超时的。

代码:

#include <iostream>  
#include <cmath>     
using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n)&&n!=0)    //可以看出来定是整数的 。 
    {
        for(int i=(int)sqrt(2*n); i>=1 ;i--)   //边界问题也是要注意。
        {
            double temp=(double)i;
            if( (2*n)/temp-(2*n)/i==0 )       //精度损失。 
            {
                int flag=(2*n-i*i)/i;
                double abc=(double)i;
                if( (2*n-abc*abc)/abc - flag!=0  )
                    continue;
                int ans=(flag+1)/2;
                if(  (flag+1)/2.0-ans!=0  )continue;
                cout<<ans<<" "<<i<<endl;    
                break;
            }         
        }
    }    
    return 0;    
}

Skyscraper相关推荐

  1. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper [模拟退火]

    P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18 ...

  2. LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  3. ACSL 美国计算机科学联赛 2016-2017 R4 摩天大楼-Skyscraper 题解

    ACSL 美国计算机科学联赛 2016-2017 R4 摩天大楼-Skyscraper 题解 转自简书App-乙酸王的简书↓↓↓ @失踪人口回归-乙酸王 好吧现在开始叫我乙酸王好了 ##前言 自从去年 ...

  4. 【题解】P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    \(Description:\) 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组. \(Sample\) \(Input:\) 4 10 5 6 3 7 3 \ ...

  5. POJ 1508 Skyscraper Floors 未完成

    描述 一个伟大的想法是建立摩天大楼!使用不是太大的土地面积,这在当今许多城市是非常昂贵的,摩天大楼提供了一个非常大的公用事业区的公寓或办公室.唯一的缺点是,它需要太长时间才能到达较高的楼层.当然,这些 ...

  6. 【USACO】摩天大楼里的奶牛Cows in a Skyscraper

    题目 https://www.luogu.org/problemnew/show/P3052 思路 状压DP 用i表示状态:奶牛是否进车厢 设f[i]为状态为i,用最少的车箱数 设g[i]为车厢最后一 ...

  7. 洛谷 P3052 USACO 摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they lovestair climbing races. A better kn ...

  8. 英语影视台词---四、Sideways

    英语影视台词---四.Sideways 一.总结 一句话总结:杯酒人生 Sideways,大致意思是"偏离.倾斜.转向-".很明显中文译名与英文原名并没有什么关联,<杯酒人生 ...

  9. 轻松练:如何从900万张图片中对600类照片进行分类|技术头条

    点击上方↑↑↑蓝字关注我们~ 「2019 Python开发者日」7折优惠最后2天,请扫码咨询 ↑↑↑ 作者 | Aleksey Bilogur 译者 | 风车云马 责编 | Jane 出品 | AI科 ...

最新文章

  1. SAP QM初阶之取样策略如何确定检验批Sample Size?
  2. python基础练习(十)
  3. ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(34)、chr(39)、chr(..
  4. c# 中Stopwatch 类的运用
  5. 文件操作-小文件复制
  6. 收藏:DPDK内存基本概念
  7. Azure DevOps Server CI - 自搭跨平台容器代理Agents
  8. wordpress房产信息网_Realia v3.1.2 wordpress房地产模板 租房网站模板
  9. 前端学习(3162):react-hello-react之react插件的安装
  10. CCNA-第九篇-OSPF下+VLAN开篇初介绍
  11. winForm combox 回传绑定数据
  12. 使用HttpWebRequest方式访问外部接口
  13. 概率中的独立和不相关
  14. Floyd 多源最短路径
  15. 主题词是计算机自动提取,汉语关键词自动转换主题词方法的步骤
  16. Named Route ‘***‘ has a default child route. When navigating to this named route
  17. GIS基础制图之地形图
  18. linux下 kafka的安装
  19. 重启服务器命令有哪些,每一个重启命令区别又是什么?
  20. mysql怎么给数据加序号_MySQL数据库之在mysql中给查询的结果添加序号列

热门文章

  1. 水平居中布局与滚动条跳动
  2. 面向对象--内部属性类型
  3. 利用gcc的__attribute__编译属性section子项构建初始化函数表【转】
  4. linux下的僵尸进程处理SIGCHLD信号【转】
  5. SQL Server跨库查询
  6. 外媒称Windows 10是一辆“广告大巴车”
  7. MySQL中文全文检索
  8. PHP-Codeigniter:实习笔记1
  9. 看美女有助于男士长寿
  10. 语义分割的时候,发的牢骚