Advertisement
tungSfer

uocsochunglonnhat

May 30th, 2022
1,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define pb push_back
  5. #define pii pair<int, int>
  6. #define pll pair<ll, ll>
  7. #define el endl
  8. #define umi unordered_map<int, int>
  9. #define umll unordered_map<ll, ll>
  10. #define all(vect) vect.begin(), vect.end()
  11. #define reset(A) memset(A, 0, sizeof(A))
  12. #define approx(n) fixed << setprecision(n)
  13.  
  14. using namespace std;
  15.  
  16. const int mod = 1e9 + 7;
  17.  
  18. void solve()
  19. {
  20.   ll n, k;
  21.   cin >> n >> k;
  22.   ll a[n];
  23.   ll dp[n + 5];
  24.   reset(dp);
  25.   for (int i = 0; i < n; i++)
  26.   {
  27.     cin >> a[i];
  28.     if (a[i] == k)
  29.       dp[i] = 1;
  30.   }
  31.   if (a[0] % k == 0)
  32.     dp[0] = 1;
  33.   for (int i = 1; i < n; i++)
  34.   {
  35.     if (__gcd(a[i], a[i - 1]) == k)
  36.     {
  37.       dp[i] = dp[i - 1] + 1;
  38.     }
  39.     else
  40.     {
  41.       dp[i] = max(dp[i], dp[i - 1]);
  42.     }
  43.   }
  44.   // for (int i = 0; i < n; i++)
  45.   // {
  46.   //   cout << dp[i] << " ";
  47.   // }
  48.   // cout << el;
  49.   if(dp[n - 1] == 0) {
  50.     cout << -1 << el;
  51.     return;
  52.   }
  53.   cout << dp[n - 1] << el;
  54. }
  55.  
  56. int main()
  57. {
  58.   int t = 1;
  59.   cin >> t;
  60.   // cin.ignore();
  61.   while (t--)
  62.   {
  63.     solve();
  64.   }
  65.   return 0;
  66. }
  67.  
  68. /*
  69. 3
  70. 8 3
  71. 6 9 7 10 12 24 36 27
  72. 4 3
  73. 2 4 6 8
  74. 4 6
  75. 1 2 3 6
  76. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement