QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#741566#9738. Make It Divisible5720226849WA 1ms5776kbC++143.7kb2024-11-13 14:39:202024-11-13 14:39:22

Judging History

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

  • [2024-11-27 18:44:44]
  • hack成功,自动添加数据
  • (/hack/1263)
  • [2024-11-14 09:10:13]
  • hack成功,自动添加数据
  • (/hack/1178)
  • [2024-11-13 14:39:22]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5776kb
  • [2024-11-13 14:39:20]
  • 提交

answer

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

// #define int long long
#define x first
#define y second 
#define ls u << 1
#define rs u << 1 | 1
#define PII pair<int,int>  

const int N = 5e4 + 10, inf = 1e18, mod1 = 998244353, mod2 = 1e9 + 7, M = 18;

int lg[N];

int gcd(int a, int b) {
    return (b == 0 ? a : gcd(b, a % b));
}

int lowbit(int x) {
    return x & -x;
}

PII pos[N];
int tr[N];

void add(int x) {
    for(; x < N; x += lowbit(x)) tr[x] ++;
}

int query(int x) {
    int res = 0;
    for(; x; x -= lowbit(x)) res += tr[x];
    return res;
}

int f[N][M];

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

struct node {
    int l, r, d;
}tr1[N * 4];

void pushup(int u) {
    tr1[u].d = gcd(tr1[ls].d, tr1[rs].d);
}

void build(int u, int l, int r) {
    tr1[u] = {l, r, 0};
    if(l == r) return;
    int mid = l + r >> 1;
    build(ls, l, mid); build(rs, mid + 1, r);
}

void modify(int u, int l, int r, int d) {
    if(tr1[u].l >= l && tr1[u].r <= r) tr1[u].d = d;
    else {
        int mid = tr1[u].l + tr1[u].r >> 1;
        if(l <= mid) modify(ls, l, r, d);
        if(r > mid) modify(rs, l, r, d);
        pushup(u);
    }
}

int query1(int u, int l, int r) {
    if(tr1[u].l >= l && tr1[u].r <= r) return tr1[u].d;
    int mid = tr1[u].l + tr1[u].r >> 1;
    if(l <= mid && r > mid) return gcd(query1(ls, l, r), query1(rs, l, r));
    else if(l <= mid) return query1(ls, l, r);
    else return query1(rs, l, r);
}

void solve() {
    int n;
    long long k; cin >> n >> k; 
    vector<int>a(n + 1); int mi = inf;
    vector<PII>vec;
    for(int i = 1; i <= n; i ++) cin >> a[i], mi = min(mi, a[i]), vec.push_back({a[i], i}), tr[i] = 0;
    sort(vec.begin(), vec.end());
    for(int i = 0; i < n; i ++) {
        vector<int>vv; vv.push_back(vec[i].y);
        while(i < n - 1 && vec[i + 1].x == vec[i].x) vv.push_back(vec[i + 1].y), i ++;
        for(int j = 0; j < vv.size(); j ++) add(vv[j]);
        for(int j = 0; j < vv.size(); j ++) {
            int l = 0, r = vv[j] - 1;
            int ss = query(vv[j]) - 1;
            while(l <= r) {
                int mid = l + r >> 1;
                if(query(mid) == ss) r = mid - 1;
                else l = mid + 1;
            }
            pos[vv[j]].x = l + 1;
            l = vv[j] + 1, r = n;
            while(l <= r) {
                int mid = l + r >> 1;
                if(query(mid) == ss) l = mid + 1;
                else r = mid - 1;
            }
            pos[vv[j]].y = r;
        }
    }
    int d = 0;
    for(int i = 1; i < n; i ++) d = gcd(d, abs(a[i] - a[i + 1]));
    if(d == 0) {
        cout << k << ' ' << k * (k + 1) / 2 << '\n';
        return;
    }
    vector<int>v;
    for(int i = 1; i * i <= d; i ++)
        if(d % i == 0) {
            v.push_back(i);
            if(i != d / i) v.push_back(d / i);
        }
    int cnt = 0;
    long long res = 0;
    for(auto x : v) {
        if(x <= mi || x - mi > k) continue;
        int y = x - mi;
        bool ok = true;
        build(1, 1, n);
        for(int i = 1; i <= n; i ++) modify(1, i, i, a[i] + y);
        for(int i = vec.size() - 1; i >= 0; i --) {
            if(query1(1, pos[vec[i].y].x, pos[vec[i].y].y) != a[vec[i].y] + y) {
                ok = false;
                break;
            }
        }
        if(ok) cnt ++, res += y;
    }
    cout << cnt << ' ' << res << '\n';
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t = 1; cin >> t;
    for(int i = 2; i < N; i ++) lg[i] = lg[i / 2] + 1;
    while(t --)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3828kb

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: -100
Wrong Answer
time: 1ms
memory: 5776kb

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
1 1
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:

wrong answer 15th lines differ - expected: '0 0', found: '1 1'