fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <stdio.h>
  4. using namespace std;
  5. long int a[100000], c[100000];
  6. int main()
  7. {
  8. long int t;
  9. scanf("\n%ld", &t);
  10. long int i, n[t];
  11. for(i=1; i <= t; i++)
  12. {
  13. long int q;
  14. scanf("%ld%ld", &n[i], &q);
  15. long int j;
  16. for(j=1; j <= n[i]; j++)
  17. {
  18. scanf("%ld", &a[j]);
  19. }
  20. long l, r;
  21. for(j=1; j <= q; j++)
  22. {
  23. long int p, count=1;
  24. scanf("%ld%ld", &l, &r);
  25. for(p=1; p <= n[i]; p++)
  26. {
  27. if(p<l || p>r)
  28. {
  29. c[count]=a[p];
  30. count++;
  31. }
  32. }
  33. long int q, remainder=1, temp;
  34. sort(c+1, c+count);
  35. for(p=1, q=2; q<count; p++, q++)
  36. {
  37. while(remainder != 0) //Finding the GCD by Euclidean Theorem.
  38. {
  39. remainder=c[q]%c[p];
  40. c[q]=c[p];
  41. c[p]=remainder;
  42. }
  43. }
  44. if(count == 2) //If we have only one element in the range then the only element will be
  45. //the gcd.
  46. printf("%ld\n", c[1]);
  47. else
  48. printf("%ld\n", c[q-1]); //As q is q++ after the for loop so q-1 and c[q-1]
  49. //due to Euclidean Theorem
  50. }
  51. }
  52. }
Success #stdin #stdout 0s 3880KB
stdin
2
3 3
2 6 9
1 1
2 2
2 3
3 3
2 6 9
1 1
2 2
2 3
stdout
3
1
2
3
1
2