fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int M = 400005;
  5. int spf[M];
  6.  
  7.  
  8. void sieveSPF() {
  9. for (int i = 0; i < M; i++) spf[i] = i;
  10.  
  11. for (int i = 2; i * i < M; i++) {
  12. if (spf[i] == i) { // i is prime
  13. for (int j = i * i; j < M; j += i) {
  14. if (spf[j] == j)
  15. spf[j] = i;
  16. }
  17. }
  18. }
  19. }
  20.  
  21. set<int> gpf(int x) {
  22. set<int> f;
  23. while (x > 1) {
  24. f.insert(spf[x]);
  25. x /= spf[x];
  26. }
  27. return f;
  28. }
  29.  
  30. int main() {
  31. ios::sync_with_stdio(false);
  32. cin.tie(nullptr);
  33.  
  34. sieveSPF();
  35.  
  36. int t;
  37. cin >> t;
  38. while (t--) {
  39. int n;
  40. cin >> n;
  41.  
  42. vector<int> a(n);
  43. for (int i = 0; i < n; i++) cin >> a[i];
  44.  
  45. string temp;
  46. getline(cin, temp);
  47.  
  48. unordered_map<int, vector<int>> d;
  49.  
  50. for (int x : a) {
  51.  
  52. for (int p : gpf(x)) {
  53. d[p].push_back(0);
  54. }
  55.  
  56. for (int p : gpf(x + 1)) {
  57. d[p].push_back(1);
  58. }
  59. }
  60.  
  61. int ans = n;
  62. for (auto &entry : d) {
  63. auto &c = entry.second;
  64. if (c.size() >= 2) {
  65. sort(c.begin(), c.end());
  66. int cost = c[0] + c[1];
  67. ans = min(ans, cost);
  68. }
  69. }
  70.  
  71. cout << ans << "\n";
  72. }
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0.01s 5284KB
stdin
6
2
1 1
1 1
2
4 8
1 1
5
1 1 727 1 1
1 1 1 1 1
2
3 11
1 1
3
2 7 11
1 1 1
3
7 12 13
1 1 1
stdout
2
1
0
1
2
1