작성일 :

문제 링크

27621번 - Sum of Three Cubes

설명

N이 주어질 때 X^3 + Y^3 + Z^3 = N을 만족하는 세 정수를 출력하는 문제입니다.


접근법

문제에서 0부터 49까지의 해가 알려져 있으므로 이를 그대로 테이블로 저장합니다.

이후 해당 값에 해가 존재하면 세 정수를 출력하고, 없으면 0을 출력합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);

    var ok = new bool[50];
    var x = new long[50];
    var y = new long[50];
    var z = new long[50];

    void Set(int i, long a, long b, long c) {
      ok[i] = true;
      x[i] = a;
      y[i] = b;
      z[i] = c;
    }

    Set(0, 0, 0, 0);
    Set(1, 0, 0, 1);
    Set(2, 0, 1, 1);
    Set(3, 1, 1, 1);
    Set(6, -1, -1, 2);
    Set(7, 0, -1, 2);
    Set(8, 0, 0, 2);
    Set(9, 0, 1, 2);
    Set(10, 1, 1, 2);
    Set(11, -2, -2, 3);
    Set(12, 7, 10, -11);
    Set(15, -1, 2, 2);
    Set(16, -511, -1609, 1626);
    Set(17, 1, 2, 2);
    Set(18, -1, -2, 3);
    Set(19, 0, -2, 3);
    Set(20, 1, -2, 3);
    Set(21, -11, -14, 16);
    Set(24, -2901096694, -15550555555, 15584139827);
    Set(25, -1, -1, 3);
    Set(26, 0, -1, 3);
    Set(27, 0, 0, 3);
    Set(28, 0, 1, 3);
    Set(29, 1, 1, 3);
    Set(30, -283059965, -2218888517, 2220422932);
    Set(33, 8866128975287528, -8778405442862239, -2736111468807040);
    Set(34, -1, 2, 3);
    Set(35, 0, 2, 3);
    Set(36, 1, 2, 3);
    Set(37, 0, -3, 4);
    Set(38, 1, -3, 4);
    Set(39, 117367, 134476, -159380);
    Set(42, -80538738812075974, 80435758145817515, 12602123297335631);
    Set(43, 2, 2, 3);
    Set(44, -5, -7, 8);
    Set(45, 2, -3, 4);
    Set(46, -2, 3, 3);
    Set(47, 6, 7, -8);
    Set(48, -23, -26, 31);

    if (!ok[n]) {
      Console.WriteLine(0);
    } else {
      Console.WriteLine($"{x[n]} {y[n]} {z[n]}");
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

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

  int n; cin >> n;
  bool ok[50] = {};
  ll x[50] = {}, y[50] = {}, z[50] = {};

  auto set = [&](int i, ll a, ll b, ll c) {
    ok[i] = true;
    x[i] = a; y[i] = b; z[i] = c;
  };

  set(0, 0, 0, 0);
  set(1, 0, 0, 1);
  set(2, 0, 1, 1);
  set(3, 1, 1, 1);
  set(6, -1, -1, 2);
  set(7, 0, -1, 2);
  set(8, 0, 0, 2);
  set(9, 0, 1, 2);
  set(10, 1, 1, 2);
  set(11, -2, -2, 3);
  set(12, 7, 10, -11);
  set(15, -1, 2, 2);
  set(16, -511, -1609, 1626);
  set(17, 1, 2, 2);
  set(18, -1, -2, 3);
  set(19, 0, -2, 3);
  set(20, 1, -2, 3);
  set(21, -11, -14, 16);
  set(24, -2901096694LL, -15550555555LL, 15584139827LL);
  set(25, -1, -1, 3);
  set(26, 0, -1, 3);
  set(27, 0, 0, 3);
  set(28, 0, 1, 3);
  set(29, 1, 1, 3);
  set(30, -283059965LL, -2218888517LL, 2220422932LL);
  set(33, 8866128975287528LL, -8778405442862239LL, -2736111468807040LL);
  set(34, -1, 2, 3);
  set(35, 0, 2, 3);
  set(36, 1, 2, 3);
  set(37, 0, -3, 4);
  set(38, 1, -3, 4);
  set(39, 117367, 134476, -159380);
  set(42, -80538738812075974LL, 80435758145817515LL, 12602123297335631LL);
  set(43, 2, 2, 3);
  set(44, -5, -7, 8);
  set(45, 2, -3, 4);
  set(46, -2, 3, 3);
  set(47, 6, 7, -8);
  set(48, -23, -26, 31);

  if (!ok[n]) cout << 0 << "\n";
  else cout << x[n] << " " << y[n] << " " << z[n] << "\n";

  return 0;
}