HDU/HDOJ 1399 Starship Hakodate-maru Asia 2001, Hakodate (Japan)

 

Starship Hakodate-maru

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

The surveyor starship Hakodate-maru is famous for her two fuel containers with unbounded capacities. They hold the same type of atomic fuel balls.
There, however, is an inconvenience. The shapes of the fuel containers #1 and #2 are always cubic and regular tetrahedral respectively. Both of the fuel containers should be either empty or filled according to their shapes. Otherwise, the fuel balls become extremely unstable and may explode in the fuel containers. Thus, the number of fuel balls for the container #1 should be a cubic number (n^3 for some n = 0, 1, 2, 3, ...) and that for the container #2 should be a tetrahedral number (n(n+1)(n+2)/6 for some n = 0, 1, 2, 3, ...).

Hakodate-maru is now at the star base Goryokaku preparing for the next mission to create a precise and detailed chart of stars and interstellar matters. Both of the fuel containers are now empty. Commander Parus of Goryokaku will soon send a message to Captain Future of Hakodate-maru on how many fuel balls Goryokaku can supply. Captain Future should quickly answer to Commander Parus on how many fuel balls she requests before her ship leaves Goryokaku. Of course, Captain Future and her officers want as many fuel balls as possible.

For example, consider the case Commander Parus offers 151200 fuel balls. If only the fuel container #1 were available (i.e. if the fuel container #2 were unavailable), at most 148877 fuel balls could be put into the fuel container since 148877 = 53 * 53 * 53 < 151200 < 54 * 54 * 54. If only the fuel container #2 were available, at most 147440 fuel balls could be put into the fuel container since 147440 = 95 * 96 * 97 / 6 < 151200 < 96 * 97 * 98 / 6. Using both of the fuel containers #1 and #2, 151200 fuel balls can be put into the fuel containers since 151200 = 39 * 39 * 39 + 81 * 82 * 83 / 6. In this case, Captain Future's answer should be "151200".

Commander Parus's offer cannot be greater than 151200 because of the capacity of the fuel storages of Goryokaku. Captain Future and her officers know that well.

You are a fuel engineer assigned to Hakodate-maru. Your duty today is to help Captain Future with calculating the number of fuel balls she should request.

Input

The input is a sequence of at most 1024 positive integers. Each line contains a single integer. The sequence is followed by a zero, which indicates the end of data and should not be treated as input. You may assume that none of the input integers is greater than 151200.

Output

The output is composed of lines, each containing a single integer. Each output integer should be the greatest integer that is the sum of a nonnegative cubic number and a nonnegative tetrahedral number and that is not greater than the corresponding input number. No other characters should appear in the output.

Sample Input

100
64
50
20
151200
0

Sample Output

99
64
47
20
151200

Source

Asia 2001, Hakodate (Japan)
 
题意:
给你一个数n,判断在n之下,最大能够表示为k^3+m*(m+1)*(m+2)/6形式的数
 
思路:
先开始,枚举m,然后验证k是不是一个三次方的数,结果TLE了。
后来想了下,发现在验证3次方的这一步太花费时间,于是想到了用预处理
可以事先预处理一个数组,里面存上i^3,这样以后在查询的时候效率就大大提高
 
算法复杂度:O(n^(3/2))
 
我的代码:
#include<stdio.h>
#include<math.h>

int triple[1000];
int num;

void init()
{
    int i;
    num=0;
    for(i=0;i*i*i<=151200;i++)
        triple[num++]=i*i*i;
}

int main()
{
    int n,t,i,j,res,ans;
    init();
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        ans=0;
        for(i=0;i*(i+1)*(i+2)/6<=n;i++)
        {
            res=0;
            t=n-i*(i+1)*(i+2)/6;
            for(j=num-1;j>=0;j--)
            {
                if(t>=triple[j])
                {
                    res=triple[j];
                    break;
                }
            }
            if(res+i*(i+1)*(i+2)/6>ans)
                ans=res+i*(i+1)*(i+2)/6;
            if(ans==n)
                break;
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
享受此扩展程序将更改您的新标签。 打开新的新标签页时,享受设计最丰富的背景,只需挑选您的壁纸,并让所有人都嫉妒! 此应用程序包含最佳的免费日本卡哇伊自然背景和壁纸的镶边选项卡的集合。 我们所有的新标签页都是经过亲自选择的,因此您可以个性化自己的chrome浏览器。 获取大量用于chrome新选项卡的FullHD Japan HD壁纸您的浏览器将使用Japan HD应用程序以新鲜的色彩播放! 我们应用程序的所有壁纸的质量扩展都是1920x1080。 每次打开新标签页时,喜欢的视图中令人愉悦的图像都会使您心情舒畅。 使用我们的应用程序,您将使您的计算机与众不同。 我们的扩展程序是免费,快速的,并且提供流行,免费和高分辨率的特色武士卡哇伊主题和背景的最佳图库。 支持语言:Bahasa Indonesia,Bahasa Melayu,Deutsch,English,English (UK),English (United States),Français,Kiswahili,Nederlands,Norsk,Tiếng Việt,Türkçe,català,dansk,eesti,español,español (Latinoamérica),hrvatski,italiano,latviešu,lietuvių,magyar,polski,português (Brasil),português (Portugal),română,slovenský,slovenščina,suomi,svenska,čeština,Ελληνικά,Српски,български,русский,українська,فارسی‎,मराठी,हिन्दी,বাংলা,தமிழ்,తెలుగు,ಕನ್ನಡ,മലയാളം,ไทย,አማርኛ,‫العربية,中文 (简体),中文 (繁體),日本語,한국어

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值