fork(1) download
  1. /*/
  2. Author: _Chaitanya1999
  3. "Everything in this world is magic, except to the magician"
  4. /*/
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8. /*/---------------------------Defines-----------------------------------------/*/
  9.  
  10. #pragma GCC optimize("Ofast")
  11. #define int long long
  12. #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
  13. #define pb push_back
  14. #define eb emplace_back
  15. #define fn for(int i =0 ;i <n;i++)
  16. #define fn1 for( int i =1;i<=n; i++)
  17. #define fm for(int j =0 ;j <m;j++)
  18. #define fm1 for(int j =1;j<=m;j++)
  19. #define fi first
  20. #define se second
  21. #define endl '\n'
  22. #define PI 3.14159265358979323846
  23. #define MOD 1000000007
  24. #define all(a) a.begin(),a.end()
  25. #define rall(a) a.rbegin(),a.rend()
  26. const int N = 2e6+5;
  27. const int INF = 1e18L;
  28. // for all eight directions
  29. int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  30. int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
  31. // for all 4 directions
  32. //int dx[4]={-1,1,0,0};
  33. //int dy[4]={0,0,-1,1};
  34. //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  35. /*/-----------------------------Debug begins----------------------------------/*/
  36. vector<string> split(const string& s, char c) {
  37. vector<string> v; stringstream ss(s); string x;
  38. while (getline(ss, x, c)) v.emplace_back(x); return move(v);
  39. }
  40. template<typename T, typename... Args>
  41. inline string arrStr(T arr, int n) {
  42. stringstream s; s << "[";
  43. for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
  44. if(n>0)
  45. s << arr[n - 1] ;
  46. s<< "]";
  47. return s.str();
  48. }
  49. #define trace(args...) {__trace_begin(__LINE__); __trace(split(#args, ',').begin(), args);}
  50. inline void __trace_begin(int line) { cerr << "#" << line << ": "; }
  51. template<typename T> inline void __trace_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
  52. template<typename T> inline void __trace_out_var(T* val) { cerr << arrStr(val, 10); }
  53. template<typename T> inline void __trace_out_var(T val) { cerr << val; }
  54. inline void __trace(vector<string>::iterator it) { cerr << endl; }
  55.  
  56. template<typename T, typename... Args>
  57. inline void __trace(vector<string>::iterator it, T a, Args... args) {
  58. cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
  59. __trace_out_var(a);
  60. cerr << "; ";
  61. __trace(++it, args...);
  62. }
  63. /*/-----------------------------Code begins----------------------------------/*/
  64. int ar[N];
  65. signed main(){
  66. IOS;
  67. int T=1;
  68. cin >> T;
  69. while(T--){
  70. int n;
  71. cin >> n ;
  72. int dp[n][3];
  73. fn{
  74. cin >> ar[i];
  75. dp[i][0]=dp[i][1]=dp[i][2]=0;
  76. }
  77. dp[0][0]=ar[0];
  78. dp[0][1]=ar[0]+1;
  79. dp[0][2]=ar[0]-1;
  80. for(int i=1;i<n;i++){
  81. dp[i][0]=max({__gcd(ar[i],dp[i-1][0]),__gcd(ar[i],dp[i-1][1]),__gcd(ar[i],dp[i-1][2])});
  82. dp[i][1]=max({__gcd(ar[i]+1,dp[i-1][0]),__gcd(ar[i]+1,dp[i-1][1]),__gcd(1+ar[i],dp[i-1][2])});
  83. dp[i][2]=max({__gcd(ar[i]-1,dp[i-1][0]),__gcd(ar[i]-1,dp[i-1][1]),__gcd(ar[i]-1,dp[i-1][2])});
  84. }
  85. int ans=max({3ll,dp[n-1][0],dp[n-1][1],dp[n-1][2]});
  86. cout << ans << endl;
  87.  
  88. }
  89. return 0;
  90. }
Runtime error #stdin #stdout 0s 4928KB
stdin
Standard input is empty
stdout
Standard output is empty