st表

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using ld = long double;

#define all(a) begin(a), end(a)
#define int ll

template <typename T, class F = function<T(const T &, const T &)>>
class sparsetable {
    public:
    int n;
    vector<vector<T>> mat;
    F func;
    sparsetable() {}
    void init(const vector<T> &a, const F &f) {
        func = f;
        n = static_cast<int>(a.size());
        int max_log = 32 - __builtin_clz(n);
        mat.resize(max_log);
        mat[0] = a;
        for (int j = 1; j < max_log; j++) {
            mat[j].resize(n - (1 << j) + 1);
            for (int i = 0; i <= n - (1 << j); i++) {
                mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
            }
        }
    }
    T get(int from, int to) const {
        assert(0 <= from && from <= to && to <= n - 1);
        int lg = 32 - __builtin_clz(to - from + 1) - 1;
        return func(mat[lg][from], mat[lg][to - (1 << lg) + 1]);
    }
};

signed main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        vector<int> b;
        for (int i = 0; i < n - 1; i++) {
            b.push_back(abs(a[i] - a[i + 1]));
        }
        n -= 1;
        sparsetable<int> st;
        st.init(b, [&](int i, int j) {return __gcd(i, j);});
        int ans = 1;
        for (int i = 0; i < n; i++) {
            while (i + ans - 1 < n && st.get(i, i + ans - 1) != 1) ans++;
        }  
        cout << ans << '\n';      
    }
    return 0;
}
上一篇:android – 实现室内地图


下一篇:Linux自启动执行脚本方法