fork download
  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <vector>
  4. #include <stdlib.h>
  5. #include <algorithm>
  6. #include <memory.h>
  7. #include <string.h>
  8. #include <math.h>
  9. #include <string>
  10. #include <algorithm>
  11. #include <set>
  12.  
  13. using namespace std;
  14. typedef long long lli;
  15. typedef vector<int> vi;
  16. typedef pair<int, int> pii;
  17.  
  18. const int MAX = 100010;
  19. const int MV = 1000001;
  20.  
  21. int t, n;
  22. int a[MAX] = {0};
  23. int u[MV] = {0};
  24. vi f[MV];
  25.  
  26. void solve() {
  27. memset(u, 0, sizeof(u));
  28. scanf("%d", &n);
  29. for(int i = 0; i < n; i++)
  30. scanf("%d", &a[i]);
  31.  
  32. int l = 0;
  33. int r = 0;
  34. int res = 0;
  35.  
  36. vi rf, lf;
  37. lf = f[a[0]];
  38. rf = f[a[0]];
  39. while(1) {
  40. bool canBeAdded = true;
  41. for(int i = 0; i < rf.size(); i++)
  42. if(u[rf[i]]) {
  43. canBeAdded = false;
  44. break;
  45. }
  46. if(canBeAdded) {
  47. for(int i = 0; i < rf.size(); i++)
  48. u[rf[i]] = 1;
  49. res = max(res, r - l + 1);
  50. r++;
  51. if(r == n)
  52. break;
  53. rf = f[a[r]];
  54. } else {
  55. for(int i = 0; i < lf.size(); i++)
  56. u[lf[i]] = 0;
  57. l++;
  58. lf = f[a[l]];
  59. }
  60. }
  61.  
  62. if(res == 1)
  63. printf("-1\n");
  64. else
  65. printf("%d\n", res);
  66. }
  67.  
  68. int main() {
  69. for(int i = 2; i <= MV; i++)
  70. if(f[i].size() == 0) {
  71. for(int j = 1; j * i <= MV; j++)
  72. f[i * j].push_back(i);
  73. }
  74.  
  75. #ifdef _LOCAL_VAN
  76. freopen("in.txt","r",stdin);
  77. #endif
  78. scanf("%d\n" ,&t);
  79.  
  80. while(t--) {
  81. solve();
  82. }
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0.69s 42600KB
stdin
3
10
3 3 10 10 2 5 14 15 8 8
3
2 2 4
2
7 2
4
2 2 3 4
stdout
2
-1
2