QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865878#9738. Make It DivisibleAngelOlanWA 1ms3712kbC++232.8kb2025-01-22 03:43:412025-01-22 03:43:41

Judging History

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

  • [2025-01-22 03:43:41]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3712kb
  • [2025-01-22 03:43:41]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
// Pura Gente del Coach Moy

using i64 = long long;

struct SparseTable {
  vector<vector<int>> jmp;
  vector<int> V;

  SparseTable(const vector<int>& V) : V(V) {
    init();
  }

  inline int op(int i, int j) {
    return V[i] <= V[j] ? i : j;
  }

  void init() {
    {
      vector<int> xd((int)V.size());
      iota(xd.begin(), xd.end(), 0);
      jmp.emplace_back(xd);
    }
    for (int pw = 1, k = 1; pw * 2 <= (int)V.size(); pw *= 2, ++k) {
      jmp.emplace_back((int)V.size() - pw * 2 + 1);
      for (int j = 0; j < (int)jmp[k].size(); ++j) {
        jmp[k][j] = op(jmp[k - 1][j], jmp[k - 1][j + pw]);
      }
    }
  }

  int query(int l, int r) { // [a, b)
    int dep = 31 - __builtin_clz(r - l);
    return op(jmp[dep][l], jmp[dep][r - (1 << dep)]);
  }
};

int buildCartesianTree(int L, int R, vector<int>& l, vector<int>& r, SparseTable& st) {
  if (L >= R) {
    return -1;
  }
  if (L + 1 == R) {
    l[L] = r[L] = L;
    return L;
  }
  int M = st.query(L, R);
  // cout << L << ' ' << R << ' ' << M << '\n';
  l[M] = buildCartesianTree(L, M, l, r, st);
  r[M] = buildCartesianTree(M + 1, R, l, r, st);
  if (l[M] == -1) {
    l[M] = M;
  }
  if (r[M] == -1) {
    r[M] = M;
  }
  return M;
}

void buildCartesianTree(int L, int R, vector<int>& l, vector<int>& r, vector<int>& a) {
  SparseTable st(a);
  buildCartesianTree(0, (int)a.size(), l, r, st);
}

bool ok(int x, vector<int>& a, vector<int>& l, vector<int>& r) {
  for (int i = 0; i < (int)a.size(); ++i) {
    if (gcd(a[l[i]] + x, a[r[i]] + x) != a[i] + x) {
      return false;
    }
  }
  return true;
}

void solve() {
  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  for (int& x : a) {
    cin >> x;
  }

  int mn = *min_element(a.begin(), a.end()), mx = *max_element(a.begin(), a.end()), d = mx - mn;
  if (!d) {
    cout << k << ' ' << ((i64)k * (k + 1) >> 1) << '\n';
    return;
  }

  vector<int> div;
  for (int i = 1; i * i <= d; ++i) {
    if (d % i == 0) {
      div.push_back(i);
      if (i != (d / i)) {
        div.push_back(d / i);
      }
    }
  }

  vector<int> l(n), r(n);
  buildCartesianTree(0, n, l, r, a);

  // for (int i : l) {
  //   cout << i << ' ';
  // }
  // cout << '\n';
  // for (int i : r) {
  //   cout << i << ' ';
  // }
  // cout << '\n';

  int ans = 0;
  i64 sum = 0;
  for (int d : div) {
    if (d > mn) {
      int x = d - mn;
      // cout << d << ' ' << x << '\n';
      if (x <= k && ok(x, a, l, r)) {
        ++ans;
        sum += x;
      }
    }
  }
  // cout << '\n';
  cout << ans << ' ' << sum << '\n';
}

int main() {
  cin.tie(0)->sync_with_stdio(0);

  int t;
  cin >> t;
  
  while (t--) {
    solve();
  }

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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 467th lines differ - expected: '1 8', found: '0 0'