-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathADAGF.cpp
More file actions
34 lines (34 loc) · 813 Bytes
/
Copy pathADAGF.cpp
File metadata and controls
34 lines (34 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/ADAGF/
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
int gcd(int a, int b) {
if (a < b) swap(a, b);
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
long long resp = 0;
int n;
scanf("%d", &n);
map<int, int> velho, novo;
for (int i = 1; i <= n; i++) {
novo.clear();
velho[0]++;
int x;
scanf("%d", &x);
for (map<int, int>::iterator it = velho.begin(); it != velho.end();
it++) {
int y = (*it).first;
int qtd = (*it).second;
int mdc = gcd(x, y);
resp += 1LL * qtd * mdc;
novo[mdc] += qtd;
}
velho = novo;
}
printf("%lld\n", resp);
return 0;
}