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++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int n;
- cin >> n;
- int a[n + 2], pre[n + 2], sup[n + 2];
- cin >> a[1];
- pre[1] = a[1];
- for (int i = 2; i <= n; i++) {
- cin >> a[i];
- pre[i] = __gcd(pre[i - 1], a[i]);
- }
- sup[n] = a[n];
- for (int i = n - 1; i >= 1; i--) {
- sup[i] = __gcd(sup[i + 1], a[i]);
- }
- int q;
- cin >> q;
- while (q--) {
- int l, r;
- cin >> l >> r;
- if (l - 1 == 0 && r + 1 <= n) {
- cout << sup[r + 1] << '\n';
- } else if (r + 1 > n && l - 1 >= 1) {
- cout << pre[l - 1] << '\n';
- } else if (r + 1 > n && l - 1 == 0) {
- cout << "0" << endl;
- } else
- cout << __gcd(pre[l - 1], sup[r + 1]) << '\n';
- }
- }
No comments