QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#642552#9426. Relearn through ReviewAndevikingTL 227ms71984kbC++202.1kb2024-10-15 14:56:322024-10-15 14:56:32

Judging History

This is the latest submission verdict.

  • [2024-10-15 14:56:32]
  • Judged
  • Verdict: TL
  • Time: 227ms
  • Memory: 71984kb
  • [2024-10-15 14:56:32]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define range(x) (x).begin(), (x).end()
const int dir[][2] = {1, 0, -1, 0, 0, 1, 0, -1};

#define int long long

const int N = 200005;
int f[2][20][N];
int n;

void pre(int f[][N], const vector<int> &a)
{
    for (int i = 1; i <= n; i++)
        f[0][i] = a[i];

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

int ask(int f[][N], int l, int r)
{
    if (r < l)
        return 0;
    int k = __lg(r - l + 1);
    return gcd(f[k][l], f[k][r - (1 << k) + 1]);
}

vector<vector<int>> v;
void get(const vector<int> &a, bool flag)
{
    pre(f[flag], a);
    for (int i = 1; i <= n; ++i) {
        int pos = i;
        int now = a[pos];

        while (pos >= 1) {
            int l = 1, r = pos;
            while (l < r) {
                int mid = (l + r) >> 1;
                if (ask(f[flag], mid, i) != now)
                    l = mid + 1;
                else
                    r = mid;
            }
            v[i].push_back(l);
            pos = l - 1;
            now = gcd(now, a[pos]);
        }
    }
}
void solve()
{
    int k;
    cin >> n >> k;
    v.clear();
    v.shrink_to_fit();
    v.resize(n + 1);
    vector<int> a(n + 5), b(n + 5);
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 1; i <= n; ++i)
        b[i] = a[i] + k;

    pre(f[0], a);
    get(b, 1);

    for (int i = 1; i <= n; ++i) {
        sort(range(v[i]));
        v[i].erase(unique(range(v[i])), v[i].end());
    }

    int mx = 0;
    for (int i = 1; i <= n; ++i) {
        // cout << v[i].size() << '\n';
        for (const auto &c : v[i]) {
            int now = gcd(ask(f[0], 1, c - 1), gcd(ask(f[1], c, i), ask(f[0], i + 1, n)));
            mx = max(mx, now);
        }
    }

    cout << mx << '\n';
}

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

    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: 2ms
memory: 11648kb

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: 0
Accepted
time: 191ms
memory: 17844kb

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:

ok 100000 lines

Test #3:

score: 0
Accepted
time: 197ms
memory: 38548kb

input:

1000
71 451750502977198411
701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 7015137...

output:

701513700102652904
628264251002959880
866034990978685601
718723820869997225
525309136656747615
453291245761058554
420366973911241294
500173849665919725
16701821680586640
794711320668492112
799961738480944637
963500289005941882
190368877908873112
973069943210898565
629019279628092667
1921616220783983...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 198ms
memory: 47800kb

input:

100
5516 16561406822518327
121909691713696369 226403713182578971 940446193219943418 505054437099599243 505054437099599243 383144745385902874 470223096609971709 714042480037364447 417976085875530408 783705161016619515 888199182485502117 208988042937765204 330897734651461573 818536501506247049 7488738...

output:

17415670244813767
678667366385241526
375190657607916623
343566816881610443
293858497297593293
545063989451911922
101584687520632945
923261939978554511
245471164671296626
996567332718295422
871411820593738277
999473841903341933
575286590792404442
298698210937205101
265822932252018295
4113318308187297...

result:

ok 100 lines

Test #5:

score: 0
Accepted
time: 219ms
memory: 56064kb

input:

10
2651 1901143105096273
954803577560210292 636535718373473528 159133929593368382 159133929593368382 954803577560210292 477401788780105146 636535718373473528 636535718373473528 954803577560210292 636535718373473528 318267859186736764 636535718373473528 636535718373473528 318267859186736764 795669647...

output:

159133929593368382
793024501989621764
454059328664071477
113729984568648330
507863600649451091
779491329333959710
821665805532903623
384579792180981183
356029896436006899
340683633028457433

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 227ms
memory: 71984kb

input:

3
31056 13873801082583029
316385357210519324 316385357210519324 316385357210519324 949156071631557972 632770714421038648 949156071631557972 316385357210519324 316385357210519324 316385357210519324 316385357210519324 632770714421038648 949156071631557972 316385357210519324 316385357210519324 31638535...

output:

316385357210519324
399693130963531970
229449205713014908

result:

ok 3 lines

Test #7:

score: -100
Time Limit Exceeded

input:

1
300000 309955051600565498
497784205512766609 995568411025533218 995568411025533218 995568411025533218 995568411025533218 497784205512766609 497784205512766609 995568411025533218 995568411025533218 995568411025533218 497784205512766609 995568411025533218 497784205512766609 497784205512766609 497784...

output:


result: