fork download
  1. import java.util.*;
  2.  
  3. // プログラミングのお題スレ Part14
  4. // mevius.5ch.net/test/read.cgi/tech/1558168409/388
  5. // 388 名前: ◆QZaw55cn4c [sage] 投稿日:2019/06/16(日) 17:58:27.24 ID:gDHKfsB6 [1/3]
  6. // お題:
  7. // a, b, c, d, e, f, g, h, i, j, x は正の整数で
  8. // a^3 - b^3 = c^3 - d^3 = e^3 - f^3 = g^3 - h^3 = i^3 - j^3 = x
  9. // を満たす.
  10. // 条件を満たす最小x と対応する a, b, c, d, e, f, g, h, i, j を求めよ
  11. //
  12. // 397 名前: ◆QZaw55cn4c [sage] 投稿日:2019/06/16(日) 22:15:01.42 ID:gDHKfsB6 [2/3]
  13. // >>388
  14. // 条件を追加します、a, b, c, d, e, f, g, h, i, j は互いに異なる正の整数です
  15. // 試算では
  16. // x = 1412774811
  17. // になりました
  18.  
  19. class Ideone
  20. {
  21. public static void main(String[] args)
  22. {
  23. System.out.println(result(findX(4)));
  24. System.out.println(result(findX(5)));
  25. }
  26.  
  27. static long calc(int a, int b)
  28. {
  29. return (long) a * a * a - (long) b * b * b;
  30. }
  31.  
  32. static long findX(int num)
  33. {
  34. HashMap<Long, Integer> map = new HashMap<>();
  35. TreeSet<Long> result = new TreeSet<>();
  36. int[] n = new int[64];
  37.  
  38. for (int i = 2; i <= 2097151; i++)
  39. {
  40. if (n.length <= i)
  41. n = Arrays.copyOf(n, n.length * 2);
  42.  
  43. map.clear();
  44. result.clear();
  45.  
  46. n[i] = i - 1;
  47. long max = calc(i, i - 1);
  48. for (int j = 2; j <= i; j++)
  49. {
  50. while (n[j] > 0)
  51. {
  52. long x = calc(j, n[j]);
  53. if (x > max) break;
  54. int count = map.compute(x, (k, v) -> v == null ? 1 : v + 1);
  55.  
  56. if (count == num) result.add(x);
  57. else if (count > num) result.remove(x);
  58.  
  59. n[j]--;
  60. }
  61. }
  62.  
  63. if (!result.isEmpty()) return result.first();
  64. }
  65. return -1;
  66. }
  67.  
  68. static String result(long x)
  69. {
  70. StringBuilder result = new StringBuilder();
  71. for (int i = 2; calc(i, i - 1) <= x; i++)
  72. {
  73. int j = i - 1;
  74. while(j > 0 && calc(i, j) < x) j--;
  75. if (calc(i, j) == x) result.append(i).append("^3-").append(j).append("^3 = ");
  76. }
  77. return result.append(x).toString();
  78. }
  79. }
Success #stdin #stdout 0.68s 2184192KB
stdin
Standard input is empty
stdout
162^3-51^3 = 165^3-72^3 = 178^3-115^3 = 678^3-675^3 = 4118877
1134^3-357^3 = 1155^3-504^3 = 1246^3-805^3 = 2115^3-2004^3 = 4746^3-4725^3 = 1412774811