QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#854584#9738. Make It Divisibleucup-team008#WA 18ms5352kbC++176.5kb2025-01-12 02:56:362025-01-12 02:56:36

Judging History

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

  • [2025-01-12 02:56:36]
  • 评测
  • 测评结果:WA
  • 用时:18ms
  • 内存:5352kb
  • [2025-01-12 02:56:36]
  • 提交

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#include <omp.h>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef int64_t ll;
typedef long double ld;

const int D = 16;
const int RAGETREE_SZ = 1 << D;
int koosaga[2 * RAGETREE_SZ]; // actual values, internal nodes are lazy
array<int, 2> koosagamin[2 * RAGETREE_SZ];
int koosagagcd[2 * RAGETREE_SZ]; // leaves - differences, internal nodes, GCD

void apply(int idx, int val) {
  koosagamin[idx][0] += val;
  koosaga[idx] += val;
}
void build(int idx) {
  assert(idx >= RAGETREE_SZ);
  while(idx > 1) {
    idx /= 2;
    koosagamin[idx] = min(koosagamin[2*idx], koosagamin[2*idx+1]);
    koosagamin[idx][0] += koosaga[idx];
  }
}
void push(int idx) {
  assert(idx >= RAGETREE_SZ);
  for(int d = D; d > 0; d--) {
    int i = idx >> d;
    apply(2*i, koosaga[i]);
    apply(2*i+1, koosaga[i]);
    koosaga[i] = 0;
  }
}
int qry(int idx) { // query single val
  idx += RAGETREE_SZ;
  push(idx);
  return koosaga[idx];
}
array<int, 2> minqry(int lhs, int rhs) { // query range
  array<int, 2> ret = {int(2e9), int(2e9)};
  lhs += RAGETREE_SZ;
  rhs += RAGETREE_SZ;
  push(lhs);
  push(rhs);
  while(lhs <= rhs) {
    if(lhs%2) ret = min(ret, koosagamin[lhs++]);
    if(rhs%2==0) ret = min(ret, koosagamin[rhs--]);
    lhs /= 2;
    rhs /= 2;
  }
  return ret;
}
int gcdqry(int lhs, int rhs) { // query range
  int ret = 0;
  lhs += RAGETREE_SZ;
  rhs += RAGETREE_SZ;
  push(lhs);
  push(rhs);
  while(lhs <= rhs) {
    if(lhs%2) ret = gcd(ret, koosagagcd[lhs++]);
    if(rhs%2==0) ret = gcd(ret, koosagagcd[rhs--]);
    lhs /= 2;
    rhs /= 2;
  }
  return ret;
}
void upd(int lhs, int rhs, int val) { // updates a range
  lhs += RAGETREE_SZ;
  rhs += RAGETREE_SZ;
  int a = lhs;
  int b = rhs;
  while(lhs <= rhs) {
    if(lhs%2) apply(lhs++, val);
    if(rhs%2 == 0) apply(rhs--, val);
    lhs /= 2;
    rhs /= 2;
  }
  build(a);
  build(b);
}
void pointupd(int idx, int val) {
  idx += RAGETREE_SZ;
  koosaga[idx] = val;
  koosagamin[idx] = {val, idx - RAGETREE_SZ};
  while(idx > 1) {
    idx /= 2;
    koosagamin[idx] = min(koosagamin[2*idx], koosagamin[2*idx+1]);
  }
}
void diffupd(int idx, int val) { // updates a difference
  idx += RAGETREE_SZ;
  koosagagcd[idx] = abs(val);
  while(idx > 1) {
    idx /= 2;
    koosagagcd[idx] = gcd(koosagagcd[2*idx], koosagagcd[2*idx+1]);
  }
}

void rsolve() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n+1);
  int smallest = 1e9;
  int largest = 1;
  for(int i = 1; i <= n; i++) {
    cin >> v[i];
    updmin(smallest, v[i]);
    updmax(largest, v[i]);
  }
  if(smallest == largest) return void(cout << k << " " << (k*(k+ll(1))/2) << "\n");
  for(int i = 1; i <= n; i++) {
    pointupd(i, v[i]);
    diffupd(i, abs(v[i] - v[i-1]));
  }
  int g = abs(v[2] - v[1]);
  for(int i = 3; i <= n; i++) g = gcd(g, abs(v[i] - v[i-1]));
  vector<int> allf;
  for(int i = 1; i * i <= g; i++) {
    if(g%i==0) {
      allf.pb(i);
      if(i*i != g) allf.pb(g/i);
    }
  }
  sort(all(allf));
  ll reta = 0, retb = 0;
  int baseline = smallest;
  auto dfs = y_combinator([&](auto self, int lhs, int rhs) -> bool {
    if(lhs >= rhs) return true;
    int gg = gcd(qry(lhs), gcdqry(lhs+1, rhs));
    array<int, 2> cand = minqry(lhs, rhs);
    if(gg != cand[0]) return false;
    return self(lhs, cand[1]-1) && self(cand[1]+1, rhs);
  });
  int orig = smallest;
  for(int out: allf) {
    if(out > orig) {
      upd(1, n, out - orig);
      orig = out;
      if(dfs(1, n)) {
        reta++;
        retb += out - baseline;
      }
    }
  }
  cout << reta << " " << retb << "\n";
}

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

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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: 9ms
memory: 5108kb

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: 9ms
memory: 5312kb

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: 9ms
memory: 5352kb

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

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
11 2194
2 6878
1 2
1 1
2 10
3 6873
1 110
0 0
1 36
13 19290
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
0 0
0 0
0 0
0 0
0 0
0 0
3 1100
1000000000 5000000005...

result:

wrong answer 4th lines differ - expected: '1 1', found: '11 2194'