hack 我的 ac 代码:
input:
4
2 1 1 1
output:
1
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 200;
int n, a[MAXN];
bool flag = 1;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
if (a[i] != 1) {
flag = 0;
break;
}
}
if (flag) {
cout << 0 << endl;
return 0;
}
int maxx = 1e9;
for (int i = 1; i < n; i++) {
int x = a[i];
for (int j = i + 1; j <= n; j++) {
x = __gcd(x, a[j]);
if (j - i > maxx) {
break;
}
if (x == 1) {
maxx = min(maxx, j - i);
break;
}
}
}
cout << (maxx == 1e9 ? -1 : n + maxx - 1);
return 0;
}
原因:只判断了全部为 1 的情况,没有判断部分为 1 的情况。