QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#623135#9426. Relearn through ReviewSlinkCompile Error//C++142.0kb2024-10-09 10:20:102024-10-09 10:20:13

Judging History

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

  • [2024-10-09 10:20:13]
  • 评测
  • [2024-10-09 10:20:10]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ll long long
#define int ll
#define vi vector<int>
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FOD(i, a, b) for (int i = a; i >= b; --i)

const int N = 3e5 + 10;
int a[N], b[N], g[N * 4];

void build(int id, int l, int r)
{
    if (l == r)
    {
        g[id] = b[l];
        return;
    }
    int mid = (l + r) / 2;
    build(id * 2, l, mid);
    build(id * 2 + 1, mid + 1, r);
    g[id] = __gcd(g[id * 2], g[id * 2 + 1]);
}

int get(int id, int l, int r, int u, int v)
{
    if (r < u || l > v)
        return 0;
    if (u <= l && r <= v)
        return g[id];
    int mid = (l + r) / 2;
    return __gcd(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}

void solve()
{
    int n, k;

    cin >> n >> k;
    int suf[n + 5] = {};
    int ans = 0;
    FOR(i, 0, n - 1)
    {
        cin >> a[i];
        ans = __gcd(ans, a[i]);
        b[i] = a[i] + k;
    }
    FOD(i, n - 1, 0) suf[i] = __gcd(suf[i + 1], a[i]);
    build(1, 0, n - 1);
    FOR(r, 0, n - 1)
    {
        int x = get(1, 0, n - 1, 0, r);
        ans = max(ans, __gcd(x, suf[r + 1]));
    }
    int gcd_left = a[0];
    FOR(l, 1, n - 1)
    {
        int lo = l, hi = n - 1, r = -1;
        while (lo <= hi)
        {
            int mid = (lo + hi) / 2;
            if (suf[mid + 1] % gcd_left)
            {
                lo = mid + 1;
            }
            else
            {
                if (suf[mid + 1] % gcd_left == 0)
                {
                    r = mid;
                    hi = mid - 1;
                }
                else
                    lo = mid + 1;
            }
        }
        ans = max(ans, __gcd(gcd_left, __gcd(get(1, 0, n - 1, l, r), suf[r + 1])));
        gcd_left = __gcd(gcd_left, a[l]);
    }
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

Details

cc1plus: error: ‘::main’ must return ‘int’