OkiBry

Bai1

Jan 14th, 2022
1,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. // Recursive function to return gcd of a and b
  8. int GCD(int a, int b){
  9.     return b == 0 ? a : GCD(b, a % b);  
  10. }
  11.  
  12. int main(){
  13.     long long  n, k;
  14.     cin >> n >> k;
  15.     vector <long long> a(n);
  16.     vector <long long> prefixGCD(n);
  17.     for (long long i = 0; i < n; i++) {
  18.         cin >> a[i];  
  19.     }
  20.     long long gcd_max = a[0];
  21.     long long start = 0, end = 0;
  22.    
  23. //______________O(kN)_________________//
  24.  
  25.     for (long long i = 0; i <= n-k; i++) {
  26.         long long gcd = 0;
  27.         for (long long j = i; j < i+k ; ++j){
  28.             gcd = GCD(gcd, a[j]);
  29.             printf("%3lld -> gcd %3lld; ",a[j],gcd);
  30.         }
  31.         if (gcd > gcd_max){
  32.             gcd_max = gcd;
  33.             start = i;
  34.             end = i+k;
  35.         }
  36.         cout<<" --> "<<gcd_max<<endl;
  37.     }
  38.  
  39.     cout << gcd_max <<endl;
  40.     cout << start << " " << end << endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment