QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#603291#9426. Relearn through Review333zhan#RE 0ms3824kbC++202.2kb2024-10-01 15:42:002024-10-01 15:42:01

Judging History

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

  • [2024-10-01 15:42:01]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3824kb
  • [2024-10-01 15:42:00]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long

using namespace std;

struct ST {
    vector <vector <int>> f;
    vector <int> lg;

    ST (int n, vector <int> a) {
        lg.resize (n + 1);
        f.assign (n + 1, vector <int> (lg[n] + 1));
        for (int i = 2; i <= n; i ++) {
            lg[i] = lg[i / 2] + 1;
        }
        for (int i = 1; i <= n; i ++) {
            f[i][0] = a[i];
        }

        for (int j = 1; j <= lg[n]; j ++) {
            for (int i = 1; i + (1 << j) - 1 <= n; i ++) {
                f[i][j] = gcd (f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
            }
        }
    } 

    int query (int l, int r) {
        int k = lg[r - l + 1];
        return gcd (f[l][k], f[r - (1 << k) + 1][k]);
    }
};

void solve () {
    int n, k;
    cin >> n >> k;

    vector <int> a (n + 1);
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
    }

    vector <int> pre (n + 2), suf (n + 2);
    pre[1] = a[1];
    suf[n] = a[n];
    for (int i = 2; i <= n; i ++) {
        pre[i] = gcd (pre[i - 1], a[i]);
    }
    for (int i = n - 1; i >= 1; i --) {
        suf[i] = gcd (suf[i + 1], a[i]);
    }

    vector <int> b (n + 1);
    for (int i = 1; i <= n; i ++) {
        b[i] = a[i] + k;
    }

    ST st (n, b);

    int maxx = 0;
    for (int i = 1; i <= n; i ++) {
        for (int j = i; j <= n; j ++) {
            int l = j, r = n;
            auto check = [&] (int x) {
                return st.query (i, x) == st.query (i, j);
            };
            while (l < r) {
                int mid = (l + r + 1) / 2;
                if (check (mid)) {
                    l = mid;
                } else {
                    r = mid - 1;
                }
            }
            int gc = st.query (i, l);
            if (i > 1) {    
                gc = gcd (gc, pre[i - 1]);
            }
            if (l < n) {
                gc = gcd (gc, suf[l + 1]);
            }
            maxx = max (maxx, gc);
            j = l;
        }
    }

    cout << maxx << '\n';
}

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

    int T = 1;
    cin >> T;

    while (T --) {
        solve ();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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:


result: