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.
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.
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.
48
9 42
6
6 6
思路:dfs分别去考虑当前最大的p是否用上了,用上了则当前体积为m-p^3,反之体积变为p^3-1,这样才使得p不会被用上。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define maxn int(1e3+20)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define PI acos(-1.0)
typedef long long ll;
typedef pair<ll ,ll> point;
ll get_max(ll n)
{
ll L = 1,R = ll(1e5+10),mid,ans = 1;
while(L <= R)
{
mid = (R + L)/2LL;
if(mid*mid*mid <= n){
L = mid+1;
ans = mid;
}
else{
R = mid-1;
}
}
return ans;
}
ll get_pow(ll p)
{
return p*p*p;
}
point ans;
void solve(ll m,ll sum,ll num)
{
if(m == 0){
ans = max(ans,make_pair(num,sum));
return;
}
ll ma = get_max(m);
solve(m - get_pow(ma),sum+get_pow(ma),num+1);
if(ma-1 >= 0){
solve(get_pow(ma) - get_pow(ma-1) -1,sum+get_pow(ma-1),num+1);
}
}
int main()
{
#ifdef CDZSC_June
freopen("t.txt", "r", stdin);
#endif
ll a;
while(~scanf("%lld",&a))
{
ans = make_pair(0,0);
solve(a,0,0);
printf("%lld %lld\n",ans.first,ans.second);
}
return 0;
}