QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#635789#9426. Relearn through Reviewxhytom#RE 0ms3972kbC++231.7kb2024-10-12 20:58:312024-10-12 20:58:33

Judging History

你现在查看的是最新测评结果

  • [2024-10-12 20:58:33]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3972kb
  • [2024-10-12 20:58:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define int long long
using i64 = long long;

struct ST {
    std::vector<std::vector<int>> st;
    int M;

    ST (std::vector<int> a) {
        const int n = a.size() - 1;
        const int M = std::log(n) + 1;
        st.assign(M + 1, std::vector<int> (n + 1));
        for (int i = 1; i <= n; i++) {
            st[0][i] = a[i];
        }

        for (int j = 1; j <= M; j++) {
            for (int i = 1; i + (1 << j) - 1 <= n; i++) {
                st[j][i] = __gcd(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
            }
        }
    }

    int query(int l, int r) {
        if (l > r) return 0;
        int j = __lg(r - l + 1);
        return __gcd(st[j][l], st[j][r - (1 << j) + 1]);
    }
};


void solve() {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
        b[i] = a[i] + k;
    }

    ST A(a), B(b);

    std::vector<int> left{1}, right{n};
    for (int i = 1; i < n; i++) {
        if (A.query(1, i) != A.query(1, i + 1)) {
            left.push_back(i + 1);
        }
    }

    for (int i = n; i >= 2; i--) {
        if (A.query(i, n) != A.query(i - 1, n)) {
            right.push_back(i - 1);
        }
    }

    int ans = A.query(1, n);

    for (auto l : left) {
        for (auto r : right) {
            if (l <= r) {
                ans = std::max(__gcd(__gcd(A.query(1, l - 1), A.query(r + 1, n)), B.query(l, r)), ans);
            }
        }
    }
    std::cout << ans << "\n";
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int t = 1;
    std::cin >> t;
    while (t --) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3972kb

input:

2
6 2
5 3 13 8 10 555
3 0
3 6 9

output:

5
3

result:

ok 2 lines

Test #2:

score: -100
Runtime Error

input:

100000
1 608611451460421713
33155506392034032
1 743116173559300609
6138108577573005
7 364454564010802125
657035115675878115 657035115675878115 657035115675878115 657035115675878115 657035115675878115 292580551665075990 657035115675878115
4 316648374341335221
365788422120542814 182894211060271407 731...

output:

641766957852455745
749254282136873614
657035115675878115
182894211060271407
880411769063535667
560553564512176618
183698346865682381
962990836390050009
616597869896951268
878097339332572161
188820994675344528
997057718507559252
949074379610491450
37337367838628559
632093288650732211
3771217139073309...

result: