QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#745660#9738. Make It DivisiblekuguadawangTL 344ms19384kbC++174.5kb2024-11-14 10:59:192024-11-14 10:59:23

Judging History

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

  • [2024-11-27 18:44:44]
  • hack成功,自动添加数据
  • (/hack/1263)
  • [2024-11-14 10:59:23]
  • 评测
  • 测评结果:TL
  • 用时:344ms
  • 内存:19384kb
  • [2024-11-14 10:59:19]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#pragma GCC optimize(3)
#define int long long
#define llu unsigned long long
#define endl "\n"
#define inf 0x3f3f3f3f
#define debug cout << "****************" << endl
using namespace __gnu_pbds;
using namespace std;

typedef pair<int, int> PII;

const int N = 1e6 + 7;

using i64 = long long;

const int SIZE = 500010;

struct SegmentTree {
    int l, r;
    long long dat;
} t[SIZE * 4];
long long a[SIZE], b[SIZE], c[SIZE];
int n;

long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}

// 维护区间gcd的线段树
void build(int p, int l, int r)
{
    t[p].l = l, t[p].r = r;
    if (l == r) {
        t[p].dat = b[l];
        return;
    }
    int mid = (l + r) / 2;
    build(p * 2, l, mid);
    build(p * 2 + 1, mid + 1, r);
    t[p].dat = gcd(t[p * 2].dat, t[p * 2 + 1].dat);
}

void change(int p, int x, long long v)
{
    if (t[p].l == t[p].r) {
        t[p].dat += v;
        return;
    }
    int mid = (t[p].l + t[p].r) / 2;
    if (x <= mid)
        change(p * 2, x, v);
    else
        change(p * 2 + 1, x, v);
    t[p].dat = gcd(t[p * 2].dat, t[p * 2 + 1].dat);
}

long long ask(int p, int l, int r)
{
    if (l <= t[p].l && r >= t[p].r)
        return abs(t[p].dat);
    int mid = (t[p].l + t[p].r) / 2;
    long long val = 0;
    if (l <= mid)
        val = gcd(val, ask(p * 2, l, r));
    if (r > mid)
        val = gcd(val, ask(p * 2 + 1, l, r));
    return abs(val);
}

long long sum(int x)
{
    long long y = 0;
    for (; x; x -= x & -x)
        y += c[x];
    return y;
}

void add(int x, long long y)
{
    for (; x <= n; x += x & -x)
        c[x] += y;
}
int ccc;
int T = 1;

void solve()
{
    ccc++;
    int k;
    cin >> n >> k;
    for (int i = 0; i <= n + 1; i++)
        c[i] = a[i] = b[i] = 0;
    vector<int> l(n + 7);
    vector<int> r(n + 7);
    stack<int> stk;
    int pd = 0;
    int ans1 = 0, ans2 = 0;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        b[i] = a[i] - a[i - 1];
    // if (ccc == 23 && T != 500) {
    //     cout << n << " " << k << endl;
    //     for (int j = 1; j <= n; j++)
    //         cout << a[j] << "%";
    //     return;
    // }
    if (n == 1) {
        cout << k << ' ' << k * (k + 1) / 2 << endl;
        return;
    }
    build(1, 1, n);
    for (int i = 1; i <= n; i++) {
        while (!stk.empty() && a[stk.top()] > a[i])
            r[stk.top()] = i - 1, stk.pop();
        stk.push(i);
    }
    while (!stk.empty()) {
        r[stk.top()] = n;
        stk.pop();
    }
    for (int i = n; i >= 1; i--) {
        while (!stk.empty() && a[stk.top()] > a[i])
            l[stk.top()] = i + 1, stk.pop();
        stk.push(i);
    }
    while (!stk.empty()) {
        l[stk.top()] = 1;
        stk.pop();
    }
    auto modify = [&](int l, int r, int x) {
        change(1, l, x);
        if (r < n)
            change(1, r + 1, -x);
        add(l, x);
        add(r + 1, -x);
    };
    auto query = [&](int l, int r) {
        long long al = a[l] + sum(l);
        long long val = l < r ? ask(1, l + 1, r) : 0;
        return (int)gcd(al, val);
    };
    for (int i = 1; i <= n; i++) {
        modify(l[i], r[i], -a[i]);
        if (query(l[i], r[i]) != 0)
            pd++;
        modify(l[i], r[i], a[i]);
    }
    if (pd == 0) {
        cout << k << ' ' << k * (k + 1) / 2 << endl;
        return;
    }
    gp_hash_table<int, int> cnt;
    build(1, 1, n);
    for (int i = 1; i <= n; i++) {
        modify(l[i], r[i], -a[i]);
        int gc = query(l[i], r[i]);
        for (int j = 1; j * j <= gc; j++) {
            if (gc % j == 0) {
                if (j - a[i] >= 1 && j - a[i] <= k) {
                    if (++cnt[j - a[i]] == pd)
                        ans1++, ans2 += j - a[i];
                }
                if (gc / j != j) {
                    if ((gc / j) - a[i] >= 1 && (gc / j) - a[i] <= k) {
                        if (++cnt[(gc / j) - a[i]] == pd)
                            ans1++, ans2 += (gc / j) - a[i];
                    }
                }
            }
        }
        modify(l[i], r[i], a[i]);
    }
    cout << ans1 << " " << ans2 << endl;
}

// 1 2 3 4 5 6 7 8 9

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    for (int j = 1; j <= T; j++)
        solve();
    return 0;
}

// 8 80 2 8 2

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 9724kb

input:

3
5 10
7 79 1 7 1
2 1000000000
1 2
1 100
1000000000

output:

3 8
0 0
100 5050

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 9792kb

input:

4
201 1000000000
1 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5...

output:

0 0
0 0
0 0
0 0

result:

ok 4 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 9784kb

input:

500
4 1000000000
8 14 24 18
4 1000000000
17 10 18 14
4 1000000000
6 17 19 19
4 1000000000
15 14 15 25
4 1000000000
16 16 5 25
4 1000000000
4 30 20 5
4 1000000000
11 4 23 9
4 1000000000
14 25 13 2
4 1000000000
18 18 1 15
4 1000000000
22 22 22 28
4 1000000000
15 17 17 10
4 1000000000
22 14 13 25
4 100...

output:

0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
...

result:

ok 500 lines

Test #4:

score: 0
Accepted
time: 44ms
memory: 17032kb

input:

1
50000 1000000000
230 286458 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 ...

output:

0 0

result:

ok single line: '0 0'

Test #5:

score: 0
Accepted
time: 37ms
memory: 19384kb

input:

1
50000 1000000000
12087 1196491 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 55314643 553146...

output:

0 0

result:

ok single line: '0 0'

Test #6:

score: 0
Accepted
time: 50ms
memory: 17140kb

input:

1
50000 1000000000
2138984 42249920 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 358123541 3581...

output:

0 0

result:

ok single line: '0 0'

Test #7:

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

input:

500
39 1000000000
75 7 7 381 41 1197 177 177 41 109 109 16 1197 177 41 381 1605 381 381 7 177 177 177 177 177 177 177 177 7 7 143 143 143 143 143 653 143 823 7
61 1000000000
327 327 327 327 405153474 327 405153474 327 810306621 810306621 810306621 810306621 810306621 810306621 810306621 810306621 81...

output:

0 0
25 631568356
13 18925862
1 1
2 6878
1 2
1 1
2 10
1 1
1 110
0 0
1 36
11 4796
1 29
1 4
6 2209209
0 0
3 8
1 2
9 30770061
1000000000 500000000500000000
1 3
1000000000 500000000500000000
0 0
1 5
1 1
6 6615501
3 8233825
2 1035
2 4
7 1288
0 0
1 3
16 1617883221
2 10
0 0
1 5739
15 102584
3 1100
100000000...

result:

ok 500 lines

Test #8:

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

input:

50
794 1000000000
32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 32994 ...

output:

4 13399
1 488
0 0
1 1
2 764
1 3762
2 3
1 4
3 704189325
1 1
7 226705168
7 2992
1 10
2 14
0 0
2 4136
1 1
5 1503264540
1 6
1 9
2 178
12 1965492
3 8622003
1 1
1 10
0 0
0 0
1 316605749
1 4
2 8
1 22
2 1452
0 0
1 47
1 1
3 11
7 709953544
20 10709552
3 8
1 3
19 84763135
2 1256
3 1268
29 1652299260
1 1
3 1124...

result:

ok 50 lines

Test #9:

score: 0
Accepted
time: 56ms
memory: 10360kb

input:

5
8181 1000000000
846711550 846711550 846711550 846711550 31870 169367806 169367806 169367806 169367806 169367806 169367806 169367806 169367806 169367806 169367806 169367806 31870 338703742 338703742 190 14224510 14224510 6526 6526 190 190 190 10 190 10 21214 21214 154 154 8578 10 190 10 10 82 3862 ...

output:

2 10
1 8
26 3610753
1 3
2 4

result:

ok 5 lines

Test #10:

score: 0
Accepted
time: 65ms
memory: 16852kb

input:

1
50000 1000000000
8376 1656 5016 20136 5016 1656 1656 1656 1656 1656 1656 1656 1656 1656 1656 16776 8376 50376 251976 1656 1656 10056 1656 1656 1656 1656 1656 1656 1656 1656 1656 1656 1656 5016 1656 3336 3336 3336 3336 3336 83976 16776 16776 16776 1656 536 1096 2216 1096 1096 67176 33576 5576 1096 ...

output:

1 24

result:

ok single line: '1 24'

Test #11:

score: 0
Accepted
time: 344ms
memory: 18684kb

input:

1
50000 1000000000
91892194 394 137838094 394 137838094 137838094 394 183783994 394 183783994 183783994 183783994 183783994 394 45946294 45946294 45946294 45946294 45946294 45946294 45946294 45946294 45946294 45946294 45946294 45946294 45946294 45946294 394 137838094 137838094 137838094 394 91892194...

output:

468 209787236

result:

ok single line: '468 209787236'

Test #12:

score: -100
Time Limit Exceeded

input:

500
100 1000000000
776155459 776155459 776155459 776155459 41021059 776155459 41021059 776155459 776155459 41021059 41021059 776155459 776155459 41021059 776155459 41021059 776155459 776155459 41021059 41021059 776155459 776155459 776155459 41021059 41021059 41021059 41021059 41021059 776155459 7761...

output:


result: