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. // hotspotうぉーみんぐあっぷ
  24. for (int i = 0; i < 2; i++) { result(findX(5)); }
  25.  
  26. long s = System.currentTimeMillis();
  27. String result4 = result(findX(4));
  28. String result5 = result(findX(5));
  29. long e = System.currentTimeMillis();
  30. System.out.printf("%s%n%s%n%,dms%n", result4, result5, e - s);
  31. }
  32.  
  33. static long calc(int a, int b)
  34. {
  35. return (long) a * a * a - (long) b * b * b;
  36. }
  37.  
  38. static long findX(int num)
  39. {
  40. HashMap<Long, Integer> map = new HashMap<>();
  41. TreeSet<Long> result = new TreeSet<>();
  42. int[] n = new int[32768];
  43. int cur = 0;
  44.  
  45. for (int i = 511; i <= 2097151; i+= 512)
  46. {
  47. if (i >= n.length)
  48. {
  49. n = Arrays.copyOf(n, n.length * 2);
  50. }
  51.  
  52. while(cur < i)
  53. {
  54. n[cur] = cur - 1;
  55. cur++;
  56. }
  57.  
  58. map.clear();
  59. result.clear();
  60.  
  61. long max = calc(i, i - 1);
  62. for (int j = 2; j <= i; j++)
  63. {
  64. while (n[j] > 0)
  65. {
  66. long x = calc(j, n[j]);
  67. if (x > max) break;
  68. int count = map.compute(x, (k, v) -> v == null ? 1 : v + 1);
  69.  
  70. if (count == num) result.add(x);
  71. else if (count > num) result.remove(x);
  72.  
  73. n[j]--;
  74. }
  75. }
  76.  
  77. if (!result.isEmpty()) return result.first();
  78. }
  79. return -1;
  80. }
  81.  
  82. static String result(long x)
  83. {
  84. StringBuilder result = new StringBuilder();
  85. for (int i = 2; calc(i, i - 1) <= x; i++)
  86. {
  87. int j = i - 1;
  88. while(j > 0 && calc(i, j) < x) j--;
  89. if (calc(i, j) == x) result.append(i).append("^3-").append(j).append("^3 = ");
  90. }
  91. return result.append(x).toString();
  92. }
  93. }
Success #stdin #stdout 0.39s 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
33ms