fork download
  1. /*
  2.   Cred : SunnyYeahBoi
  3.   It's my last chance (⌐■_■)
  4.   Problem :
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. #define int long long
  12. #define double long double
  13. #define endl "\n"
  14. #define NAME "a"
  15.  
  16. const int MAXN = 1e6 + 5;
  17. const int inf = 1e18;
  18. const int MOD = 1e9 + 7;
  19.  
  20. void FileInput(){
  21. if(fopen(NAME".inp" , "r") == NULL)
  22. freopen(NAME".inp" , "w" , stdout);
  23. freopen(NAME".inp" , "r" , stdin);
  24. freopen(NAME".out" , "w" , stdout);
  25. }
  26.  
  27. int n , k;
  28. int a[MAXN];
  29. int pref[MAXN] , suf[MAXN];
  30.  
  31. void solve(){
  32. cin >> n >> k;
  33. for(int i = 1 ; i <= n ; i++) cin >> a[i];
  34.  
  35. pref[1] = a[1]; // pref[i] - gcd(a[1] , a[2] , ... , a[i])
  36. suf[n] = a[n]; // suf[i] - gcd(a[i] , a[i + 1] , ... , a[n])
  37.  
  38. for(int i = 2 ; i <= n ; i++)
  39. pref[i] = __gcd(pref[i - 1] , a[i]);
  40. for(int i = n - 1 ; i > 0 ; i--)
  41. suf[i] = __gcd(suf[i + 1] , a[i]);
  42.  
  43. int res = -inf;
  44. for(int i = 1 ; i + k - 1 <= n ; i++){
  45. int gc = 0;
  46.  
  47. if(i == 1)
  48. gc = suf[i + k];
  49. else if(i + k - 1 == n)
  50. gc = pref[i - 1];
  51. else gc = __gcd(pref[i - 1] , suf[i + k]);
  52.  
  53. res = max(res , gc);
  54. }
  55.  
  56. cout << res << endl;
  57. }
  58.  
  59. int32_t main(){
  60. FileInput();
  61. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  62. int t = 1;
  63. // cin >> t;
  64. while(t--)
  65. solve();
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 5848KB
stdin
Standard input is empty
stdout
Standard output is empty