fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int gcd(int a, int b) {
  5. return b == 0 ? a : gcd(b, a%b);
  6. }
  7.  
  8. int main() {
  9. int n, m, sum;
  10. cin >> n;
  11. while(n-- > 0) {
  12. cin >> m;
  13. int ns[m];
  14. for(int i = 0; i < m; i++) cin >> ns[i];
  15. sum = 0;
  16. for(int i = 0; i < m; i++) {
  17. for(int j = i + 1; j < m; j++) sum += gcd(ns[i], ns[j]);
  18. }
  19. cout << sum << endl;
  20. }
  21. }
Success #stdin #stdout 0s 4952KB
stdin
4
4 5 6 7 8
4 8 6 2 9
3 2 15 6
5 12 25 29 19 11
stdout
7
11
6
10