Problem:
Fermat's Last Theorem
In the 17th century, Fermat wrote that he proved for any integer $n \geq 3$,
there exist no positive integers $x$, $y$, $z$ such that
$x^n + y^n = z^n$.
However he never disclosed the proof.  Later, this claim was named
Fermat's Last Theorem or Fermat's Conjecture.
If Fermat's Last Theorem holds in case of $n$, then it also holds
in case of any multiple of $n$.
Thus it suffices to prove cases where $n$ is a prime number
and the special case $n$ = 4.
A proof for the case $n$ = 4 was found in Fermat's own memorandum.
The case $n$ = 3 was proved by Euler in the 18th century.
After that, many mathematicians attacked Fermat's Last Theorem.
Some of them proved some part of the theorem, which was a
partial success.
Many others obtained nothing.
It was a long history.
Finally, Wiles proved Fermat's Last Theorem in 1994.
Fermat's Last Theorem implies that for any integers $n \geq 3$
and $z > 1$, it always holds that
$z^n > $ max { $x^n + y^n | x > 0, y > 0, x^n + y^n \leq z^n$ }.
Your mission is to write a program that verifies this in the case
$n$ = 3 for a given $z$.  Your program should read in
integer numbers greater than 1, and, corresponding to each input
$z$, it should output the following:
$z^3 - $ max { $x^3 + y^3 | x > 0, y > 0, x^3 + y^3 \leq z^3$ }.
Input
The input is a sequence of lines each containing one positive integer
number followed by a line containing a zero.  You may assume that all of
the input integers are greater than 1 and less than 1111.
Output
The output should consist of lines each containing a single
integer number.  Each output integer should be
$z^3 - $ max { $x^3 + y^3 | x > 0, y > 0, x^3 + y^3 \leq z^3$ }.
for the corresponding input integer
z
.
No other characters should appear in any output line.
Sample Input
6
4
2
0
Output for the Sample Input
27
10
6

Submitted solutions:

code_snippet: #include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b)
{
    return a>b;
}
ll a[maxn];
int main()
{
    #ifndef ONLINE_JUDGE
    //freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    for(int i=1;i<=1111;i++)
		a[i]=(i*i*i);
	int n;
	while(cin>>n&&n){
		ll t=n*n*n;
		ll Max=-1;
		for(int i=n;i>=1;i--)
		for(int j=n;j>=1;j--){
			if(a[i]+a[j]<=t){Max=max(a[i]+a[j],Max);}
		}
		cout<<t-Max<<endl;
	}
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3440
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
int main(){
    while(1){
        ll z;cin>>z;
        if(!z)return 0;
        ll ans=0;
        FOR(x,1,z+1)for(ll y=1;x*x*x+y*y*y<z*z*z;y++)ans=max(ans,x*x*x+y*y*y);
        cout<<z*z*z-ans<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3420
final_rank: 18

code_snippet: #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll z_cube = z * z * z;  // Pre-calculating z's cube
    ll ans = 0;
    for (ll x = 1; x * x * x <= z_cube; x++) {
      ll x_cube = x * x * x;  // Storing x's cube
      for (ll y = x; x_cube + y * y * y <= z_cube; y++) {  // y starts from x
        ll y_cube = y * y * y;  // Storing y's cube
        ans = max(ans, x_cube + y_cube);
      }
    }
    cout << z_cube - ans << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 2980
final_rank: 1

code_snippet: #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll zCube = z * z * z;  // Calculate z^3 once
    ll ans = 0;

    for (ll x = 1, xCube = x * x * x; xCube <= zCube; ++x, xCube = x * x * x) {
      for (ll y = 1, yCube = y * y * y; yCube + xCube <= zCube; ++y, yCube = y * y * y) {
        ans = max(ans, xCube + yCube);
      }
    }

    cout << zCube - ans << endl;
  }
}
accuracy: 1
time_ms: 30
memory_kb: 3092
final_rank: 12

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll ans = 0;
    ll z_cube = z * z * z;
    for (ll x = 1; x * x * x <= z_cube; x++) {
      ll x_cube = x * x * x;
      for (ll y = x; y * y * y + x_cube <= z_cube; y++) {
        ll y_cube = y * y * y;
        ans = max(ans, y_cube + x_cube);
      }
    }
    cout << z_cube - ans << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3076
final_rank: 3

code_snippet: #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll ans = 0;
    ll zCubed = z * z * z;  // Store the cube of z in a variable
    for (ll x = 1; x * x * x <= zCubed; x++)
      for (ll y = 1; y * y * y + x * x * x <= zCubed; y++)
        ans = max(ans, x * x * x + y * y * y);
    cout << zCubed - ans << endl;  // Use the stored value of z cubed
  }
}
accuracy: 1
time_ms: 30
memory_kb: 3020
final_rank: 6

code_snippet: #include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll ans = 0;
    ll z_cubed = z * z * z; // cube of z calculated once and stored in variable
    ll x, y;
    for (x = 1; x * x * x <= z_cubed; x++) {
      ll x_cubed = x * x * x; // cube of x calculated once and stored in variable
      for (y = x; y * y * y + x_cubed <= z_cubed; y++) { // y loop starts from x
        ll y_cubed = y * y * y; // cube of y calculated once and stored in variable
        ans = max(ans, x_cubed + y_cubed);
      }
    }
    cout << z_cubed - ans << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3076
final_rank: 3

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll ans = 0;
    ll cube_z = z * z * z;
    for (ll x = 1; x * x * x <= cube_z; x++)
      for (ll y = x; y * y * y + x * x * x <= cube_z; y++)
        ans = max(ans, x * x * x + y * y * y);
    cout << cube_z - ans << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3076
final_rank: 3

code_snippet: #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll ans = 0;
    ll z_cubed = z * z * z;  
    ll limit = cbrt(z_cubed);  

    for (ll x = 1; x <= limit; x++) {
      ll x_cubed = x * x * x;

      for (ll y = 1; y <= limit; y++) {
        ll y_cubed = y * y * y;

        if (x_cubed + y_cubed <= z_cubed)
          ans = max(ans, x_cubed + y_cubed);
        else
          break;  
      }
    }
    cout << z_cubed - ans << endl;
  }
}
accuracy: 1
time_ms: 30
memory_kb: 3128
final_rank: 14

code_snippet: #include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll ans = 0;
    ll zCube = z * z * z; // Calculate z^3 once
    ll limit = cbrt(zCube); // Calculate the cube root of z^3 once
    for (ll x = 1; x <= limit; x++) {
      ll xCube = x * x * x; // Calculate x^3 once
      for (ll y = 1; y <= limit; y++) {
        ll yCube = y * y * y; // Calculate y^3 once
        if (xCube + yCube <= zCube)
          ans = max(ans, xCube + yCube);
        else
          break; // Break the loop if the sum exceeds z^3
      }
    }
    cout << zCube - ans << endl;
  }
}
accuracy: 1
time_ms: 30
memory_kb: 3108
final_rank: 13

code_snippet: #include <iostream>
#include <cmath>
using namespace std;

int main() {
  int tb[1112], ans[1112], N;
  
  // Pre-compute and store cubes of numbers in tb array
  for (int i = 1; i < 1112; i++) {
    tb[i] = i * i * i;
  }

  // Pre-compute and store the smallest difference in ans array
  for (int z = 1; z < 1112; z++) {
    int z3 = tb[z], ret = z3;

    // Calculate the upper limit for i
    int max_i = cbrt(z3 / 2);
    for (int i = 1; i <= max_i; i++) {
      int i3 = tb[i];

      // Find the greatest cube number less than or equal to z3 - i3
      int j3 = tb[(int)cbrt(z3 - i3)];
      if (i3 + j3 >= z3)
        break;

      ret = min(ret, z3 - i3 - j3);
    }
    ans[z] = ret;
  }

  // Print the smallest difference for each N read from standard input
  while (cin >> N, N) {
    cout << ans[N] << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3244
final_rank: 15

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;

int nr(int a) {
  int ret = 1;
  for (int i = 2; i < 1112; i++) {
    int tb = i * i * i;
    if (tb > a)
      break;
    ret = i;
  }
  return ret * ret * ret;
}

int calc(int z) {
  int z3 = z * z * z, ret = z3;
  for (int i = 1; i < z; i++) {
    int i3 = i * i * i;
    if (z3 - i3 <= i3)
      break;
    int j3 = nr(z3 - i3);
    ret = min(ret, z3 - i3 - j3);
  }
  return ret;
}

int main() {
  int ans[1112];
  for (int i = 1; i < 1112; i++)
    ans[i] = calc(i);
  int N;
  while (cin >> N, N)
    cout << ans[N] << endl;
}
accuracy: 1
time_ms: 170
memory_kb: 3068
final_rank: 20

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;

int tb[1112], ans[1112], N;

int nr(int a) {
  int low = 1, high = 1112, mid;

  while (low < high) {
    mid = low + (high - low) / 2;
    if (tb[mid] > a)
      high = mid;
    else
      low = mid + 1;
  }

  return tb[low - 1];
}

int calc(int z) {
  int z3 = z * z * z, ret = z3;
  for (int i = 1; i < z; i++) {
    int i3 = i * i * i, j3 = nr(z3 - i3);
    if (i3 + j3 >= z3)
        break;
    ret = min(ret, z3 - i3 - j3);
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  for (int i = 1; i < 1112; i++)
    tb[i] = i * i * i;
  for (int i = 1; i < 1112; i++)
    ans[i] = calc(i);
  while (cin >> N, N)
    cout << ans[N] << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3136
final_rank: 11

code_snippet: #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll z;
  while (cin >> z && z) {
    ll ans = 0;
    ll z_cubed = z * z * z;
    ll max_x = cbrt(z_cubed); // Compute the cube root of z_cubed
    ll max_y = cbrt(z_cubed - 1); // Compute the cube root of z_cubed - 1

    for (ll x = 1; x <= max_x; x++) {
      ll x_cubed = x * x * x;
      ll max_y_with_x = cbrt(z_cubed - x_cubed);
      for (ll y = 1; y <= max_y_with_x; y++) {
        ll y_cubed = y * y * y;
        ll sum_cubed = x_cubed + y_cubed;
        if (sum_cubed <= z_cubed && sum_cubed > ans) {
          ans = sum_cubed;
        }
      }
    }

    cout << z_cubed - ans << endl;
  }
}
accuracy: 1
time_ms: 30
memory_kb: 3200
final_rank: 16

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>

const int UPPER_BOUND = 1112;

std::vector<int> tb(UPPER_BOUND);
std::vector<int> ans(UPPER_BOUND);

int nr(int a) {
  int left = 1, right = UPPER_BOUND - 1;
  while (left < right) {
    int mid = left + (right - left + 1) / 2;
    if (tb[mid] <= a) {
      left = mid;
    } else {
      right = mid - 1;
    }
  }
  return tb[left];
}

int calc(int z) {
  int z3 = z * z * z;
  int ret = z3;

  int left = 1, right = UPPER_BOUND - 1;
  while (left < right) {
    int i3 = left * left * left;
    int j3 = nr(z3 - i3);

    if (i3 + j3 >= z3) {
      break;
    }

    ret = std::min(ret, z3 - i3 - j3);
    left++;
  }

  return ret;
}

int main() {
  for (int i = 1; i < UPPER_BOUND; i++) {
    tb[i] = i * i * i;
  }

  for (int i = 1; i < UPPER_BOUND; i++) {
    ans[i] = calc(i);
  }

  int N;
  while (std::cin >> N && N) {
    std::cout << ans[N] << std::endl;
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3080
final_rank: 7

code_snippet: #include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int MAXN = 1112;
int tb[MAXN], ans[MAXN], N;

int nr(int a) {
  int left = 0, right = MAXN, mid;
  while (left < right) {
    mid = left + (right - left) / 2;
    if (tb[mid] <= a)
      left = mid + 1;
    else
      right = mid;
  }
  return tb[left - 1];
}

int calc(int z) {
  int z3 = z * z * z, ret = z3;
  for (int i = 1, i3 = i * i * i; i3 < z3; i++, i3 = i * i * i) {
    int j3 = nr(z3 - i3);
    if (i3 + j3 >= z3)
      break;
    ret = min(ret, z3 - i3 - j3);
  }
  return ret;
}

int main() {
  for (int i = 1; i < MAXN; i++)
    tb[i] = i * i * i;
  for (int i = 1; i < MAXN; i++)
    ans[i] = calc(i);
  while (cin >> N, N)
    cout << ans[N] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3004
final_rank: 2

code_snippet: #include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;

int tb[1112], ans[1112], N;

int nr(int a) {
  int ret = cbrt(a);  // Use in-built function to calculate cube root
  return tb[ret];
}

int calc(int z) {
  int z3 = z * z * z, ret = z3;
  int i3 = 1;
  for (int i = 1; i < z; i++) {
    int j3 = nr(z3 - i3);
    int sum = i3 + j3;
    if (sum >= z3)
      break;
    ret = min(ret, z3 - sum);
    i3 += (3*i*i + 3*i + 1);  // Calculate the next cube
  }
  return ret;
}

int main() {
  for (int i = 1; i < 1112; i++)
    tb[i] = i * i * i;
  for (int i = 1; i < 1112; i++)
    ans[i] = calc(i);
  while (cin >> N, N)
    cout << ans[N] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3268
final_rank: 17

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
int tb[1113], ans[1113], N;

int nr(int a) {
    int left = 2, right = 1111;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (tb[mid] > a)
            right = mid;
        else
            left = mid + 1;
    }
    return tb[left - 1];
}

int calc(int z) {
    int z3 = tb[z], ret = z3;
    for (int i = 1; i < z; i++) {
        int i3 = tb[i], j3 = nr(z3 - i3);
        if (i3 + j3 >= z3)
            break;
        ret = min(ret, z3 - i3 - j3);
    }
    return ret;
}

int main() {
    ios::sync_with_stdio(false);  // speed up cin and cout
    for (int i = 1; i <= 1112; i++)
        tb[i] = i * i * i;
    for (int i = 1; i <= 1112; i++)
        ans[i] = calc(i);
    while (cin >> N, N)
        cout << ans[N] << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3096
final_rank: 10

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
int tb[1112], ans[1112], N;

int calc(int z) {
  int z3 = z * z * z, ret = z3;
  for (int i = 1; i < z; i++) {
    int i3 = i * i * i;
    int j3 = tb[lower_bound(tb, tb+1112, z3 - i3) - tb - 1];
    if (i3 + j3 >= z3)
      break;
    if (ret > z3 - i3 - j3)
      ret = z3 - i3 - j3;
  }
  return ret;
}

int main() {
  for (int i = 1; i < 1112; i++)
    tb[i] = i * i * i;
  for (int i = 1; i < 1112; i++)
    ans[i] = calc(i);
  while (cin >> N, N)
    cout << ans[N] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3080
final_rank: 7

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
int tb[1112], ans[1112];

int calc(int z) {
  int z3 = z * z * z, ret = z3;
  for (int i = 1; i < z; i++) {
    int i3 = i * i * i;
    int j3 = *(upper_bound(tb, tb + 1112, z3 - i3) - 1);
    if (i3 + j3 >= z3)
      break;
    ret = min(ret, z3 - i3 - j3);
  }
  return ret;
}

void init() {
  for (int i = 1; i < 1112; i++)
    tb[i] = i * i * i;
  for (int i = 1; i < 1112; i++)
    ans[i] = calc(i);
}

int main() {
  init();
  int N;
  while (cin >> N, N)
    cout << ans[N] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3080
final_rank: 7