QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#759110#9738. Make It DivisibleFalse0099WA 1ms5868kbC++204.3kb2024-11-17 21:43:342024-11-17 21:43:35

Judging History

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

  • [2024-11-27 18:44:44]
  • hack成功,自动添加数据
  • (/hack/1263)
  • [2024-11-17 21:43:35]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5868kb
  • [2024-11-17 21:43:34]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
int INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
typedef pair<int, int> PII;

void init() {}

const int N = 1e5 + 10;
int a[N];

class SegmentTree
{
public:
    struct Node
    {
        int l, r;
        int sum;
    } tr[N << 2];

    Node merge(Node x, Node y)
    {
        if (x.l == -1)
            return y;
        if (y.l == -1)
            return x;
        Node z;
        z.l = min(x.l, y.l);
        z.r = max(x.r, y.r);
        z.sum = __gcd(x.sum, y.sum);
        return z;
    }

    void pushup(int u)
    {
        tr[u] = merge(tr[u << 1], tr[u << 1 | 1]);
    }

    void build(int u, int l, int r)
    {
        tr[u] = {l, r, 0};
        if (l == r)
            return;
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }

    void update(int u, int p, int x)
    {
        if (tr[u].l == tr[u].r)
        {
            tr[u].sum = x;
            return;
        }
        int mid = tr[u].l + tr[u].r >> 1;
        if (p <= mid)
            update(u << 1, p, x);
        else
            update(u << 1 | 1, p, x);
        pushup(u);
    }

    Node query(int u, int l, int r)
    {
        if (tr[u].l >= l && tr[u].r <= r)
            return tr[u];
        int mid = tr[u].l + tr[u].r >> 1;
        Node ret;
        ret.l = -1;
        if (l <= mid)
            ret = query(u << 1, l, r);
        if (r > mid)
            ret = merge(ret, query(u << 1 | 1, l, r));
        return ret;
    }
} t;

void solve()
{
    int n, l;
    cin >> n >> l;
    // vector<int> d(n + 1);
    int global_gcd = 0;
    int min_value = INF;
    t.build(1, 1, n);

    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        min_value = min(min_value, a[i]);
        //  d[i] = abs(a[i] - a[i - 1]);
        t.update(1, i, abs(a[i] - a[i - 1]));
        if (i != 1)
            global_gcd = __gcd(global_gcd, abs(a[i] - a[i - 1]));
    }

    vector<int> left_bound(n + 1);
    vector<int> right_bound(n + 1);
    stack<int> stk, stk2;

    for (int i = 1; i <= n; i++)
    {
        while (!stk.empty() && a[stk.top()] >= a[i])
            stk.pop();
        left_bound[i] = stk.empty() ? 1 : stk.top() + 1;
        stk.push(i);
    }

    for (int i = n; i >= 1; i--)
    {
        while (!stk2.empty() && a[stk2.top()] >= a[i])
            stk2.pop();
        right_bound[i] = stk2.empty() ? n : stk2.top() - 1;
        stk2.push(i);
    }

    unordered_set<int> valid_k;
    if (global_gcd == 0)
    {
        cout << l << " " << (1 + l) * l / 2 << endl;
        return;
    }

    int sum = 0;
    for (int i = 1; i * i <= global_gcd; i++)
    {
        if (global_gcd % i == 0)
        {
            int u = i;
            int k = u - min_value;
            if (k > 0 && k <= l && __gcd(a[1] + k, global_gcd) == u)
            {
                valid_k.insert(k);
                sum += k;
            }
            int v = global_gcd / i;
            k = v - min_value;
            if (k > 0 && k <= l && __gcd(a[1] + k, global_gcd) == v)
            {
                valid_k.insert(k);
                sum += k;
            }
        }
    }
    if (valid_k.size() == 0)
    {
        cout << 0 << " " << 0 << endl;
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        if (left_bound[i] != right_bound[i] || !((left_bound[i] != 1 && right_bound[i] != n)))
        {
            int ggcd = t.query(1, left_bound[i] + 1, right_bound[i]).sum;
            int max_left = a[left_bound[i]];
            int min_num = a[i];
            unordered_set<int> new_valid_k = valid_k;
            for (auto k : valid_k)
            {
                if (__gcd(ggcd, max_left + k) != min_num + k)
                {
                    new_valid_k.erase(k);
                }
            }
            valid_k = new_valid_k;
        }
    }
    cout << valid_k.size() << " ";
    int total_sum = 0;
    for (auto k : valid_k)
        total_sum += k;
    cout << total_sum << endl;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 3536kb

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: 5612kb

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:

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