-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path892C. Pride.cpp
58 lines (48 loc) · 1 KB
/
892C. Pride.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Idea:
- Greedy.
- If there is one 1 in the array then the answer is `n - 1`.
- If there is no odd number in the array then the answer is `-1`.
- Else try from each index i to find the shortest consequtive subarray
such that the GCD of it equal to 1, then the answer will be the `length
of the subarray + n - 1`.
*/
#include <bits/stdc++.h>
using namespace std;
int const N = 2001;
int n, a[N];
int main() {
bool even = true;
int one = 0;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d", a + i);
one += a[i] == 1;
if(a[i] & 1)
even = false;
}
if(one > 0) {
cout << n - one << endl;
return 0;
}
if(even) {
cout << -1 << endl;
return 0;
}
int mn = 1e9;
for(int i = 0; i < n; ++i) {
int gcd = a[i];
for(int j = i + 1; j < n; ++j) {
gcd = __gcd(gcd, a[j]);
if(gcd == 1) {
mn = min(mn, j - i);
break;
}
}
}
if(mn == 1e9)
cout << -1 << endl;
else
printf("%d\n", mn + n - 1);
return 0;
}