fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <array>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. const int n = 5000;
  9. const int m = 5;
  10. const int Dmax = n + 100;
  11.  
  12. int D, D2, itor[Dmax] = {}, itor2[Dmax], rtoi[Dmax], rtoi2[Dmax];
  13. long long d[n];
  14. int p[n];
  15.  
  16. int main(void)
  17. {
  18. int a, b, g, i, j, k, r, r2, ra, rb;
  19.  
  20. for (D2 = n + 1, k = 0; k < 2; k++) {
  21. for (; D2 <= Dmax; D2++) {
  22. fill(rtoi2, rtoi2 + D2, 0);
  23. for (i = 1; i <= n; i++) {
  24. r = (long long)i * i * i % D2;
  25. if (rtoi2[r]) break; else rtoi2[r] = i;
  26. }
  27. if (i > n) break;
  28. }
  29. if (D2 > Dmax) return cout << "D not found." << endl, -1;
  30. if (k == 0) D = D2++, copy(rtoi2, rtoi2 + D, rtoi);
  31. }
  32. for (r = 0; r < D; r++) itor[rtoi[r]] = r;
  33. for (r = 0; r < D2; r++) itor2[rtoi2[r]] = r;
  34.  
  35. vector<vector<array<int, 2>>> AB(D2);
  36. vector<int> R2(D2);
  37.  
  38. for (r = 1; r < D; r++) {
  39. for (r2 = 0; r2 < D2; r2++) AB[r2].clear();
  40. R2.clear();
  41. for (a = 2; a <= n; a++) {
  42. ra = itor[a];
  43. if ((rb = ra - r) < 0) rb += D;
  44. if (b = rtoi[rb], b == 0 || b >= a) continue;
  45. if ((r2 = itor2[a] - itor2[b]) < 0) r2 += D2;
  46. AB[r2].push_back({a, b});
  47. if (AB[r2].size() == m) R2.push_back(r2);
  48. }
  49.  
  50. for (g = 0; g < R2.size(); g++) {
  51. auto &ab = AB[R2[g]];
  52. for (k = 0, i = 0; i < ab.size(); i++) {
  53. a = ab[i][0];
  54. b = ab[i][1];
  55. d[k] = (long long)a * a * a - (long long)b * b * b;
  56. p[k] = k;
  57. k++;
  58. }
  59. sort(p, p + k, [](int x, int y) {return d[x] < d[y];});
  60.  
  61. for (i = 0; i < k - m + 1; i++) {
  62. if (d[p[i]] != d[p[i + m - 1]]) continue;
  63.  
  64. cout << d[p[i]] << ": ";
  65. for (j = i + m; j < k && d[p[j]] == d[p[i]]; j++);
  66. sort(p + i, p + j, [&](int x, int y) {return ab[x][0] < ab[y][0];});
  67. for_each(p + i, p + j, [&](int &x) {
  68. a = ab[x][0];
  69. b = ab[x][1];
  70. cout << (&x > p + i ? ", (" : "(") << a << ", " << b << ")";
  71. });
  72. cout << endl;
  73. i = j - 1;
  74. }
  75. }
  76. }
  77. return 0;
  78. }
Success #stdin #stdout 0.38s 5300KB
stdin
Standard input is empty
stdout
1412774811: (1134, 357), (1155, 504), (1246, 805), (2115, 2004), (4746, 4725)