原题链接
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
官方题解:
679B - Bear and Tower of Cubes
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
#define MOD 1000000007
#define maxn 200005
#define INF 1e18
using namespace std;
typedef long long ll;
ll d[100005];
struct Node{
Node(ll a, ll b){
k1 = a;
k2 = b;
}
ll k1, k2;
};
Node dfs(ll n, int h){
if(n <= 7){
return Node(n, h+n);
}
int hh = lower_bound(d, d+100001, n) - d;
if(d[hh] == n){
n--;
if(n <= 7)
return Node(n, h+n);
}
hh--;
Node e1 = dfs(n-d[hh], h+1);
e1.k1 += d[hh];
Node e2 = dfs(d[hh]-d[hh-1]-1, h+1);
e2.k1 += d[hh-1];
if(e1.k2 >= e2.k2)
return e1;
return e2;
}
int main(){
// freopen("in.txt", "r", stdin);
for(int i = 1; i <= 100001; i++)
d[i] = (ll)i * i * i;
ll n;
scanf("%I64d", &n);
Node e = dfs(n, 0);
printf("%I64d %I64d\n", e.k2, e.k1);
return 0;
}