QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#874892#9738. Make It Divisiblewly09TL 0ms3584kbC++206.3kb2025-01-28 19:17:542025-01-28 19:17:55

Judging History

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

  • [2025-01-28 19:17:55]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3584kb
  • [2025-01-28 19:17:54]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define MYBUF (1 << 20)
char buf[MYBUF], *p1, *p2;
#define gc()                                                                   \
  (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MYBUF, stdin), p1 == p2)       \
       ? EOF                                                                   \
       : *p1++)
inline int64_t rd() {
  int64_t x = 0;
  char c = gc();
  while (c < '0' || c > '9')
    c = gc();
  for (; c >= '0' && c <= '9'; c = gc())
    x = x * 10 + (c ^ '0');
  return x;
}
char pbuf[MYBUF], *pp = pbuf;
inline void pc(const char &c) {
  if (pp - pbuf == MYBUF)
    fwrite(pbuf, 1, MYBUF, stdout), pp = pbuf;
  *pp++ = c;
}
inline void wt(int64_t x) {
  static int sta[35];
  int top = 0;
  do {
    sta[top++] = x % 10, x /= 10;
  } while (x);
  while (top)
    pc(sta[--top] + '0');
}
#undef MYBUF

static minstd_rand rnd(chrono::steady_clock::now().time_since_epoch().count());

template <typename T> struct Min {
  inline T operator()(const T &a, const T &b) const { return std::min(a, b); }
};

template <typename T> struct Gcd {
  inline T operator()(const T &a, const T &b) const { return std::gcd(a, b); }
};

inline size_t fast_log2(size_t x) { return 63 - __builtin_clzll(x); }

template <class T, class Merge> struct sparse_table {
private:
  Merge merge;
  std::vector<std::vector<T>> v;

public:
  sparse_table(const std::vector<T> &arr) {
    size_t n = arr.size();
    size_t d = fast_log2(n) + 1;
    v.reserve(d);
    v.emplace_back(arr);
    for (size_t k = 1; k < d; ++k) {
      v.emplace_back();
      v[k].reserve(n - (1ull << k) + 1);
      for (size_t i = 0; i <= n - (1ull << k); ++i)
        v[k].push_back(merge(v[k - 1][i], v[k - 1][i + (1ull << (k - 1))]));
    }
  }
  T query(size_t l, size_t r) const {
    assert(l < r);
    size_t k = fast_log2(r - l);
    return merge(v[k][l], v[k][r - (1ull << k)]);
  }
};

int64_t quickPow(int64_t a, int64_t b, int64_t p) {
  int64_t res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = (__int128)res * a % p;
    a = (__int128)a * a % p;
  }
  return res;
}

int base_millerRabin[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};

bool millerRabin(int64_t n) {
  if (n < 3 || !(n & 1))
    return n == 2;
  int64_t u = n - 1, t = 0;
  while (!(u & 1))
    u >>= 1, ++t;
  for (int i = 0; i < 7; ++i) {
    int a = base_millerRabin[i] % n;
    if (a == 0)
      continue;
    int64_t v = quickPow(a, u, n);
    if (v == 1)
      continue;
    int64_t s;
    for (s = 0; s < t; ++s) {
      if (v == n - 1)
        break;
      v = (__int128)v * v % n;
    }
    if (s == t)
      return false;
  }
  return true;
}

int64_t Pollard_Rho(int64_t x) {
  if (!(x & 1))
    return 2;
  if (x % 3 == 0)
    return 3;
  if (x % 5 == 0)
    return 5;
  if (x % 1001)
    return 1001;
  int64_t s = 0, t = 0;
  int64_t c = rnd() % (x - 1) + 1;
  int step = 0, goal = 1;
  int64_t val = 1;
  for (goal = 1;; goal <<= 1, s = t, val = 1) {
    for (step = 1; step <= goal; ++step) {
      t = ((__int128)t * t + c) % x;
      val = (__int128)val * abs(t - s) % x;
      if ((step % 127) == 0) {
        int64_t d = gcd(val, x);
        if (d > 1)
          return d;
      }
    }
    int64_t d = gcd(val, x);
    if (d > 1)
      return d;
  }
}

void factoring(int64_t x, int64_t cnt, map<int64_t, int64_t> &ump) {
  if (x < 2)
    return;
  if (millerRabin(x)) {
    ump[x] += cnt;
    return;
  }
  int64_t ct = 0;
  int64_t p = x;
  while (p >= x)
    p = Pollard_Rho(x);
  for (; x % p == 0; ++ct)
    x /= p;
  factoring(x, cnt, ump);
  factoring(p, ct * cnt, ump);
}

#define Limit 1000000
vector<int64_t> factoring(int64_t x) {
  vector<int64_t> res;
  if (x > Limit) {
    map<int64_t, int64_t> ump;
    factoring(x, 1, ump);
    res.push_back(1);
    for (auto [p, c] : ump) {
      size_t v = p;
      size_t end = res.size();
      for (size_t i = 0; i < c; ++i, v *= p)
        for (size_t j = 0; j < end; ++j)
          res.push_back(res[j] * v);
    }
  } else {
    for (int64_t i = 1; i * i <= x; ++i)
      if (x % i == 0) {
        res.push_back(i);
        if (i * i != x)
          res.push_back(x / i);
      }
  }
  return res;
}
#undef Limit

void solve() {
  size_t n = rd(), k = rd();
  vector<int64_t> a;
  a.reserve(n);
  for (size_t i = 0, x; i < n; ++i) {
    x = rd();
    if (a.size() == 0 || a.back() != x)
      a.push_back(x);
  }
  a.shrink_to_fit();
  n = a.size();
  if (n == 1)
    return wt(k), pc(' '), wt((k + 1) * k / 2), pc('\n');
  vector<int64_t> df(n);
  df[0] = a[0];
  for (size_t i = 1; i < n; ++i)
    df[i] = abs(a[i] - a[i - 1]);
  sparse_table<int64_t, Min<int64_t>> min_a(a);
  sparse_table<int64_t, Gcd<int64_t>> gcd_d(df);
  auto range_sub_gcd = [&](size_t l, size_t r, int64_t sub) -> int64_t {
    return r - l == 1 ? a[l] - sub : gcd(gcd_d.query(l + 1, r), a[l] - sub);
  };
  auto bin_left = [&](size_t x) -> size_t {
    size_t l = 0, r = x;
    while (l < r) {
      size_t m = ((r - l) >> 1) + l;
      if (min_a.query(m, x + 1) == a[x])
        r = m;
      else
        l = m + 1;
    }
    return l;
  };
  auto bin_right = [&](size_t x) -> size_t {
    size_t l = x + 1, r = n;
    while (l < r) {
      size_t m = ((r - l + 1) >> 1) + l;
      if (min_a.query(x, m) == a[x])
        l = m;
      else
        r = m - 1;
    }
    return l;
  };
  bool flag = false;
  set<int64_t> last;
  for (size_t i = 0; i < n; ++i) {
    size_t l = bin_left(i), r = bin_right(i);
    if (r - l <= 1)
      continue;
    auto g = range_sub_gcd(l, r, a[i]);
    auto f = factoring(g);
    if (!flag) {
      for (int64_t j : f)
        if (a[i] < j && j <= a[i] + k)
          last.insert(j - a[i]);
      flag = true;
    } else {
      set<int64_t> tmp;
      for (int64_t j : f)
        if (a[i] < j && j <= a[i] + k)
          if (last.count(j - a[i]))
            tmp.insert(j - a[i]);
      swap(last, tmp);
    }
  }
  int64_t sum = 0;
  for (auto i : last)
    sum += i;
  wt(last.size()), pc(' '), wt(sum), pc('\n');
}

int main() {
  int t = rd();
  while (t--)
    solve();
  return fwrite(pbuf, 1, pp - pbuf, stdout), 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: 3584kb

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

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: -100
Time Limit Exceeded

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:


result: