Advertisement
pasholnahuy

Untitled

Jun 14th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. constexpr int len = 320;
  5.  
  6. int gcd(int a, int b) {
  7. while (a != 0 && b != 0) {
  8. if (a < b) {
  9. swap(a, b);
  10. }
  11. a = a % b;
  12. }
  13. return max(a, b);
  14. }
  15.  
  16. int main() {
  17. int n;
  18. cin >> n;
  19. vector<int> vec(n);
  20. vector<int> block_gcd(n / len + 1, 0);
  21. for (int i = 0; i < n; ++i) {
  22. cin >> vec[i];
  23. block_gcd[i / len] = gcd(vec[i], block_gcd[i / len]);
  24. }
  25. int m;
  26. cin >> m;
  27. for (int i = 0; i < m; ++i) {
  28. int l, r;
  29. cin >> l >> r, --l, --r;
  30. int start = l / len;
  31. int finish = r / len;
  32. int cur_gcd = 0;
  33. if (l % len != 0){
  34. ++start;
  35. }
  36. if ((r + 1) % len != 0) {
  37. --finish;
  38. }
  39. for (int j = start; j <= finish; ++j) {
  40. cur_gcd = gcd(cur_gcd, block_gcd[j]);
  41. }
  42. for (int i = l; i < min(r + 1, start * len); ++i) {
  43. cur_gcd = gcd(cur_gcd, vec[i]);
  44. }
  45. for (int i = max(l, (finish + 1) * len); i <= r; ++i) {
  46. cur_gcd = gcd(cur_gcd, vec[i]);
  47. }
  48. cout << cur_gcd << " ";
  49. }
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement