Starship Hakodate-maru
Description
The surveyor
starship Hakodate-maru is famous for her two fuel containers with
unbounded capacities. They told 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
indicate 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 be appear in
the output.
Sample Input
100
64
50
20
151200
0
Sample Output
99
64
47
20
151200
题意:对于给出的n,找x和y使得x*x*x + y * (y+1) * (y+2) <= n(如果不等,取最靠近n的值)代码:
#include<stdio.h>
__int64 x, y, z;
__int64 cubic(__int64 num)
{
__int64 l = 1, h = num, m;
while (l <= h)
{
m = (l + h ) / 2;
x = m * m * m;
if (x > num)
{
h = m - 1;
}
else if (x < num)
{
l = m + 1;
}
else return m;
}
return l - 1;
}
__int64 tetrahedral(__int64 num)
{
__int64 l = 1, h = num, m;
while (l <= h)
{
m = (l + h ) / 2;
y = m * (m + 1) * (m + 2) / 6;
if (y > num)
{
h = m - 1;
}
else if (y < num)
{
l = m + 1;
}
else return m;
}
return l - 1;
}
__int64 both(__int64 a, __int64 b, __int64 num)
{
__int64 i, j, z, pre = a;
if (pre < b)
{
pre = b;
}
for (i = b; i >= 0; i--)
{
for (j = a; j >= 0; j--)
{
z = j * j * j + i * (i + 1) * (i + 2) / 6;
if (z == num)
{
return z;
}
else if (z < num)
{
if (z > pre)
{
pre = z;
}
else if (j == a)//因为i是单调递减,而j每次循环初始化为最大,但j最大时仍有z<pre则结束
{
return pre;
}
}
}
}
return pre;
}
int main()
{
__int64 n, a, b, c;
while (scanf("%I64d", &n), n)
{
if(a = cubic(n), x == n)
{
printf("%I64d\n", x);
}
else if(b = tetrahedral(n), y == n)
{
printf("%I64d\n", y);
}
else
{
c = both(a, b, n);
printf("%I64d\n", c);
}
//printf("%I64d %I64d\n", a, b);
}
system("pause");
return 0;
}