fork download
  1.  
  2. #include<bits/stdc++.h>
  3. #define li long long int
  4. #define ld long double
  5. #define all(v) v.begin(),v.end()
  6. #define rev(a) reverse(all(a))
  7. #define sort(a) sort(all(a))
  8. #define pb push_back
  9. #define INF 1e18+10
  10. #define MINF -1e18-10
  11. #define rep(i,a,b) for(li i=a;i<b;i++)
  12. #define vli vector<li>
  13. using namespace std;
  14.  
  15.  
  16. template <class T> void read(T& x){
  17. cin>>x;
  18. }
  19.  
  20. template <class T, class... U> void read1(T& x, U&... u){
  21. read(x);
  22. read(u...);
  23. }
  24.  
  25. template <class A> void read1(vector<A>& v){
  26. for(auto &it:v)
  27. read(it);
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34. void solve()
  35. {
  36. li n;
  37. cin>>n;
  38.  
  39. vli v(n);
  40. read1(v);
  41.  
  42. li gcd1 = 0;
  43. gcd1 = v[0];
  44. rep(i,0,n){
  45. if(i+2 <= n-1){
  46. gcd1 = __gcd(gcd1,v[i+2]);
  47. }
  48. i+=1;
  49. }
  50.  
  51. li gcd2 = 0;
  52. if(n>1)
  53. gcd2 = v[1];
  54. rep(i,1,n){
  55. if(i+2 <= n-1){
  56. gcd2 = __gcd(gcd2,v[i+2]);
  57. }
  58. i+=1;
  59. }
  60.  
  61.  
  62.  
  63. if(gcd1==gcd2){
  64. cout<<0<<endl;
  65. }
  66. else{
  67. bool t1 = false, t2 = false, t3 = false;
  68.  
  69. rep(i,0,n){
  70. if(i%2==0 and v[i]%gcd1==0 ){
  71. t1 = true;
  72. }
  73. if(i%2 and v[i]%gcd1==0){
  74. t2 = true;
  75. }
  76. }
  77.  
  78. if(t2 and t1)
  79. t3 = true;
  80. else if(gcd1>1){
  81. cout<<gcd1<<endl;
  82. return;
  83. }
  84.  
  85. t1 = false,t2 = false;
  86. rep(i,0,n){
  87. if(i%2==0 and v[i]%gcd2==0 ){
  88. t1 = true;
  89. }
  90. if(i%2 and v[i]%gcd2==0){
  91. t2 = true;
  92. }
  93. }
  94.  
  95. if(t1 and t3 and t2)
  96. cout<<0<<endl;
  97. else if(gcd2>1)
  98. cout<<gcd2<<endl;
  99.  
  100. }
  101. }
  102.  
  103. int main()
  104. {
  105. ios_base::sync_with_stdio(false);
  106. cin.tie(0);
  107. cout.tie(0);
  108.  
  109. #ifndef ONLINE_JUDGE
  110. freopen("inputf.in", "r", stdin);
  111. freopen("outputf.in", "w", stdout);
  112. #endif
  113.  
  114.  
  115. li tc;
  116. cin>>tc;
  117. rep(i,0,tc){
  118. solve();
  119. }
  120.  
  121. return 0;
  122. }
Success #stdin #stdout 0s 5524KB
stdin
5
5
1 2 3 4 5
3
10 5 15
3
100 10 200
10
9 8 2 6 6 2 8 6 5 4
2
1 3
stdout
2
0
100
0
3