time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

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

【题解】



递归求解;

一层层地减少问题的规模;

类似分治的思想;

dfs(int w);表示m=w的时候问题的解是什么;

一开始的时候调用dfs(m);

然后,对于”当前”的w(也即这个子问题中的m);

我们有两种决策

1.直接取最大的;

则我们找到最大的p;

满足p^3<=w;

然后体积递增p^3,方块数递增1,递归求解dfs(w-p^3);

也即m变成了w-p^3,体积可以是1..w-p^3中的任意的最优解;

2.不直接取最大的;换成取中间的某个数字;

对p考虑;

这里的p仍然是上面的p;

我们可以递归求解dfs(p^3-1);

这样,下一层递归里面选最大的就会变成选(p-1)^3;

再往下递归一层就是dfs(p^3-1 - (p-1)^3);

而当我们递归dfs(p^3-1 - (p-1)^3)的时候,方块的数目也只是1;

而dfs(p^3-1 - (p-1)^3)不一定就比dfs(w-p^3)(调用时方块数也是1)大,则我们先比较一下这两个值的大小。如果前者大于后者。则说明前者有可能弄出来更优的解。否则的话就没必要递归dfs(p^3-1);

那有没有可能调用递归dfs((p-1)^3-1)呢?答案是否定的;

因为

f(p) = p^3 - 1 - (p-1)^3在0到正无穷上是一个单调递增的函数;

则dfs((p-1)^3-(p-2)^3)肯定不能弄出比dfs(p^3-1-(p-1)^3)更优的答案;

快速找出那个p可以用二分;

还不理解就多想想吧。

这题想了挺久的。上面也只是我自己的理解。

#include <cstdio>
#include <map>
#include <vector>
#define LL long long using namespace std; LL m;
pair <LL, LL> ans;
map <LL, pair<LL, LL> > fre; LL get_max(LL x)
{
LL l = 1, r = (1e5 + 10);
LL ans;
while (l <= r)
{
LL mid = (l + r) >> 1;
LL t = mid*mid*mid;
if (t <= x)
{
ans = t;
l = mid + 1;
}
else
r = mid - 1;
}
return ans;
} pair <LL, LL> dfs(LL x)
{
if (x == 0)
return make_pair(0, 0);
if (fre.count(x))
return fre[x];
pair <LL, LL> &temp = fre[x];
LL p3 = get_max(x);
temp.first = 0;
temp = dfs(x - p3);
temp.first++;
temp.second += p3;
pair <LL, LL> temp2;
LL t2 = 0;
if (p3 - 1 > 0)
{
t2 = p3 - 1 - get_max(p3 - 1);
}
if (t2 > (x - p3))
temp2 = dfs(p3 - 1);
else
temp2.first = 0;
if (temp2.first > temp.first)
temp = temp2;
return temp;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%I64d", &m);
ans = dfs(m);
printf("%I64d %I64d\n", ans.first, ans.second);
return 0;
}

【19.05%】【codeforces 680D】Bear and Tower of Cubes的更多相关文章

  1. codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)

    题目链接: D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes i ...

  2. 【CodeForces】679 B. Bear and Tower of Cubes

    [题目]B. Bear and Tower of Cubes [题意]有若干积木体积为1^3,2^3,...k^3,对于一个总体积X要求每次贪心地取<=X的最大积木拼上去(每个只能取一次)最后总 ...

  3. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  4. Codeforces 680D - Bear and Tower of Cubes

    680D - Bear and Tower of Cubes 思路:dfs+贪心,设剩余的体积为res,存在a,使得a3 ≤ res,每次取边长为a的立方体或者边长为a-1的立方体(这时体积上限变成a ...

  5. Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs

    D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...

  6. 【19.05%】【codeforces 731F】 Video Cards

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【2018.05.11 智能驾驶/汽车电子】非技术向:关于Simulink和AutoSar的几种观点

    最近看到几篇关于Simulink及AutoSar的Blog和Paper,感觉比较有意思,转载备忘之. 1. 看衰Simulink及AutoSar From:Tumiz的技术天地 https://blo ...

  8. 【2018.05.10 智能驾驶/汽车电子】AutoSar Database-ARXML及Vector Database-DBC的对比

    最近使用python-canmatrix对can通信矩阵进行编辑转换时,发现arxml可以很容易转换为dbc,而dbc转arxml却需要费一番周折,需要额外处理添加一些信息. 注意:这里存疑,还是需要 ...

  9. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

随机推荐

  1. mysql操作手册

    开启日志:https://segmentfault.com/a/1190000003072237 常用词:  Mysql:一种免费的跨平台的数据库系统  E:\mysql:表示是在dos 命令窗口下面 ...

  2. Codeforces 432C

    题目链接 题意: 给出一个长度为n(n<=10^5)的数组a,  数组a是数字1到n的一种排列(打乱顺序). 每次可以选择两个数(不同)进行交换, 但是交换的条件是被选择的两个数的下标之差加1应 ...

  3. 【JZOJ4888】【NOIP2016提高A组集训第14场11.12】最近公共祖先

    题目描述 YJC最近在学习树的有关知识.今天,他遇到了这么一个概念:最近公共祖先.对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. ...

  4. MySQL运算符和函数

    运算符 1.算数运算符 加(+):mysql> SELECT 1+1; 减(-):mysql> SELECT 3-2; 乘(*):mysql> SELECT 2*3; 除(/):my ...

  5. UVa 495【大数加法】

    UVa 495 求第n位斐波那契数列,n<=5000. 还是大数问题,这次是大数加法.仿照UVa 623的解法来做.623位数可以一位一位的增,但是这个需要预先给够位数,要是按六位存一个数组元素 ...

  6. python字符串、元组常用操作

    常用字符串操作函数: #Author:CGQ name="I \tam ChenGuoQiang" print(name.capitalize())#首字母大写,其他都小写 pri ...

  7. 阿里开源!轻量级深度学习端侧推理引擎 MNN

    阿里妹导读:近日,阿里正式开源轻量级深度学习端侧推理引擎“MNN”. AI科学家贾扬清如此评价道:“与 Tensorflow.Caffe2 等同时覆盖训练和推理的通用框架相比,MNN 更注重在推理时的 ...

  8. 1 项目里面如何打印log日志

    1  首先写一个logging.py文件 import logging from conf import setting #配置文件,里面有日志存放路径 def mylog(): logger = l ...

  9. 20172018-acmicpc-southeastern-european-regional-programming-contest-seerc-2017-en A - Concerts

    题意就是给一个字母序列a,以及一个另外一个字母序列b,你需要b中找到字母序列a,并且要求对于在b中的字母序列a,每个单词都需要满足相应的距离 其实很简单,我们利用DP[i][j]代表a已经匹配i个位置 ...

  10. @bzoj - 3836@ [Poi2014]Tourism

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个n个点,m条边的无向图,其中你在第i个点建立旅游站点的费 ...