fork download
  1. //Solution By SlavicG
  2. #include "bits/stdc++.h"
  3. using namespace std;
  4.  
  5. #define ll long long
  6.  
  7. #define forn(i,n) for(int i=0;i<n;i++)
  8. #define all(v) v.begin(), v.end()
  9. #define rall(v) v.rbegin(),v.rend()
  10.  
  11. #define pb push_back
  12. #define sz(a) (int)a.size()
  13.  
  14. #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  15. #define endl "\n"
  16.  
  17. int gcd(int a, int b)
  18. {
  19. if (a==0) return b;
  20. if (b == 0)return a;
  21. if (a == b)return a;
  22. if (a > b)return gcd(a-b, b);
  23. return gcd(a, b-a);
  24. }
  25. int main()
  26. {
  27. int n,k;
  28. cin >> n >> k;
  29. int a[n];
  30. for(int i = 0;i < n;i++)
  31. cin >> a[i];
  32.  
  33. if(k%2==0){
  34. cout << "NO\n";
  35. return 0;
  36. }
  37.  
  38. int prefixGcd[n],suffixGcd[n];
  39. prefixGcd[0] = a[0];
  40. suffixGcd[n-1] = a[n-1];
  41.  
  42. for(int i = 1 ;i < n;i++){
  43. prefixGcd[i] = gcd(prefixGcd[i-1],a[i]);
  44. }
  45. for(int i = n-2;i>=0;i--){
  46. suffixGcd[i] = gcd(suffixGcd[i+1],a[i]);
  47. }
  48.  
  49. for(int i = 1;i < n - 1 ;i++){
  50. if(gcd(prefixGcd[i-1], suffixGcd[i+1]) > 1){
  51. cout << "YES\n";
  52. return 0;
  53. }
  54. }
  55. //Check for the element at index 0 and for the element at index n-1.
  56. if(suffixGcd[1] > 1 || prefixGcd[n-2] > 1){
  57. cout << "YES\n";
  58. return 0;
  59. }
  60. cout << "NO\n";
  61. }
Time limit exceeded #stdin #stdout 5s 4516KB
stdin
Standard input is empty
stdout
Standard output is empty