Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- const long long INF = 1e9 + 7;
- const int MAXN = 200 + 10;
- const int N = 1e5 + 10;
- struct GCD_on_window {
- stack<pair<int, int>> l, r;
- int max_size;
- GCD_on_window(int k) {
- max_size = k;
- }
- void Clear() {
- while (!l.empty()) {
- if (r.empty()) {
- r.emplace(l.top().first, l.top().first);
- } else {
- r.emplace(l.top().first, __gcd(l.top().first, r.top().second));
- }
- l.pop();
- }
- }
- void Delete() {
- if (r.empty()) {
- Clear();
- }
- r.pop();
- }
- void Push(int x) {
- int g = x;
- if (r.empty()) {
- Clear();
- }
- if (!l.empty()) {
- g = __gcd(x, l.top().second);
- }
- if (l.size() + r.size() + 1 > max_size) {
- Delete();
- }
- l.emplace(x, g);
- }
- int GetGCD() {
- int x = (l.empty()? -1: l.top().second);
- int y = (r.empty()? -1: r.top().second);
- if (x == -1) {
- return y;
- }
- if (y == -1) {
- return x;
- }
- return __gcd(x, y);
- }
- };
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- vector<int> a(n);
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- GCD_on_window st(n);
- int ans = INF;
- int l = 0;
- for (int r = 0; r < n; ++r) {
- st.Push(a[r]);
- while (st.GetGCD() == 1 && l <= r) {
- ans = min(ans, r - l + 1);
- st.Delete();
- ++l;
- }
- }
- cout << (ans == INF ? -1 : ans) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment