Algorithm
[BOJ] 9613번 : GCD 합
rudgh99_algo
2024. 10. 3. 09:45
1. problem :
https://www.acmicpc.net/problem/9613
2. solution 1 :
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long ans = 0;
int a[105];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) ans += gcd(a[i], a[j]);
}
cout << ans << "\n";
}
}
gcd를 직접 구현하여, 문제를 풀었다.
ans는 100억이 될 수 있기 때문에, long long으로 잡아야 한다.