Islamic Universtiy,Bangladesh TopH 9th CPU CSE Programming Contest H. GCD Solution



 Problem Link :  https://toph.co/arena?contest=maadpgu#!/p/6474e358d47a320767bfdff1

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int32_t main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. int n;
  8. cin >> n;
  9. int a[n + 2], pre[n + 2], sup[n + 2];
  10. cin >> a[1];
  11. pre[1] = a[1];
  12. for (int i = 2; i <= n; i++) {
  13. cin >> a[i];
  14. pre[i] = __gcd(pre[i - 1], a[i]);
  15. }
  16. sup[n] = a[n];
  17. for (int i = n - 1; i >= 1; i--) {
  18. sup[i] = __gcd(sup[i + 1], a[i]);
  19. }
  20. int q;
  21. cin >> q;
  22. while (q--) {
  23. int l, r;
  24. cin >> l >> r;
  25. if (l - 1 == 0 && r + 1 <= n) {
  26. cout << sup[r + 1] << '\n';
  27. } else if (r + 1 > n && l - 1 >= 1) {
  28. cout << pre[l - 1] << '\n';
  29. } else if (r + 1 > n && l - 1 == 0) {
  30. cout << "0" << endl;
  31. } else
  32. cout << __gcd(pre[l - 1], sup[r + 1]) << '\n';
  33. }
  34. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.