UPC 在组队训练 Skyscraper(数学)

本文介绍了一种算法,用于解决在一栋拥有多个楼层的摩天大楼中,如何找到连续楼层的租赁方案,使得租金总额恰好等于给定预算的问题。算法通过优化计算过程,避免了超时,利用求和公式快速找出满足条件的楼层组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题 C: Skyscraper
时间限制: 1 Sec 内存限制: 128 MB
提交: 72 解决: 53
[提交] [状态] [讨论版] [命题人:admin]
题目描述

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.
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 < 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
题意大概就是公差为1的数列,找一个a1,数列长度为cnt,使得该数列的和正好等于输入的n。

在处理的时候如果一个个试探相加跟定会超时,所以,减少计算,由求和公式 得到a1*cnt+cnt*(cnt-1)/2=n;化简的2*a1*cnt+cnt*cnt-cnt-2*n=0;==>2*a1+cnt-1-2*n=0;未知量由两个cnt和a1,这里根据数据的大小选择试探cnt的值,a1由计算得到。
由公式的计算性质可知符合条件的cnt能够整除2*n;并且(2*n+1-cnt)%2==0;并且这里的(2*n+1-cnt)/2>=2,因为根据实际条件a1最小为1。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        int cnt=0,temp=0;
        for(int i=50000;i>0;i--)
        {
            if(2*n%i==0&&(2*n/i+1-i)%2==0&&2*n/i+1-i>=2)
            {
                temp=(2*n/i+1-i)/2;
                cnt=i;
                break;
            }
        }
        printf("%d %d\n",temp,cnt);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值