CF680D:Bear and Tower of Cubes(二分 + 数学)

D. Bear and Tower of Cubes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.

A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, ..., ak has the total volume a13 + a23 + ... + ak3.

Limak is going to build a tower. First, he asks you to tell him a positive integer X — the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.

Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.

Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≤ m that results this number of blocks.

Input

The only line of the input contains one integer m (1 ≤ m ≤ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.

Output

Print two integers — the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.

Examples
input
48
output
9 42
input
6
output
6 6
Note

In the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.

In more detail, after choosing X = 42 the process of building a tower is:

  • Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.
  • The second added block has side 2, so the remaining volume is 15 - 8 = 7.
  • Finally, Limak adds 7 blocks with side 1, one by one.

So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7·13 = 27 + 8 + 7 = 42.

题意:给一个m<=10^15,每次都减最接近当前值的立方数,让你找一个不大于m的最大的数并且这个数是减法次数最多的数。

官方:Let’s find the maximum a that a3 ≤ m. Then, it’s optimal to choose X that the first block will have side a or a - 1. Let’s see why

if the first block has side a then we are left with m2 = m - first_block = m - a3. 
If the first block has side a - 1 then the initial X must be at most a3 - 1 (because otherwise we would take a block with side a), so we are left with m2 = a3 - 1 - first_block = a3 - 1 - (a - 1)3 
If the first blocks has side a - 2 then the initial X must be at most (a - 1)3 - 1, so we are left with m2 = (a - 1)3 - 1 - first_block = (a - 1)3 - 1 - (a - 2)3.

We want to first maximize the number of blocks we can get with new limit m2. Secondarily, we want to have the biggest initial X. You can analyze the described above cases and see that the first block with side (a - 2)3 must be a worse choice than (a - 1)3. It’s because we start with smaller X and we are left with smaller m2. The situation for even smaller side of the first block would be even worse.

Now, you can notice that the answer will be small. From m of magnitude a3 after one block we get m2 of magnitude a2. So, from m we Go to m2 / 3, which means that the answer is O(loglog(m)). The exact maximum answer turns out to be 18.

The intended solution is to use the recursion and brutally check both cases: taking a3 and taking (a - 1)3 where a is maximum that a3 ≤ m. It’s so fast that you can even find a in O(m1 / 3), increasing a by one.

# include <iostream>
# include <cstdio>
# include <algorithm>
# define LL long long
using namespace std;
LL a[100010];
pair<LL, LL>ans;
void DFS(LL now, LL cnt, LL sum)
{
    if(!now)
    {
        ans = max(ans, make_pair(cnt, sum));
        return;
    }
    int pos = upper_bound(a+1, a+100002, now) - a;
    --pos;
    DFS(now - a[pos], cnt+1, sum+a[pos]);
    if(pos >1)
        DFS(a[pos]-a[pos-1]-1, cnt+1, sum+a[pos-1]);
}
int main()
{
    LL p;
    scanf("%I64d",&p);
    for(LL i=1; i<=100002; ++i)
        a[i] = i*i*i;
    DFS(p, 0, 0);
    printf("%I64d %I64d\n",ans.first, ans.second);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值