QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#768346 | #9738. Make It Divisible | i_wish_a_girl_friend | WA | 2ms | 9776kb | C++20 | 2.9kb | 2024-11-21 09:30:52 | 2024-11-21 09:31:04 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
array<int, 2> dp_min[17][(int)5e4 + 1];
unordered_set<int> st, nst;
void Solve() {
st.clear();
int n;
ll k;
cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
auto ck = [&]() {
for (int i = 2;i <= n;i++) {
if (a[i] != a[i - 1]) {
return 0;
}
}
return 1;
};
if (ck()) {
cout << k << " " << k * (k + 1) / 2 << endl;
return;
}
int lgn = __lg(n) + 1;
for (int i = 1; i <= n; i++) {
dp_min[0][i] = (array<int, 2>){ a[i], i };
}
for (int i = 1; i <= lgn; i++) {
for (int j = 1; j + (1 << i) - 1 <= n; j++) {
auto [A, B] = dp_min[i - 1][j];
auto [C, D] = dp_min[i - 1][j + (1 << i - 1)];
auto& aux = dp_min[i][j];
if (A <= C)
aux = { A, B };
else
aux = { C, D };
}
}
auto ask_pos = [&](int l, int r) {
int kk = __lg(r - l + 1);
auto [A, B] = dp_min[kk][l];
auto [C, D] = dp_min[kk][r - (1 << kk) + 1];
if (A <= C)
return B;
else
return D;
};
auto dfs = [&](auto&& dfs, int l, int r)->int {
if (l >= r) {
return 0;
}
int mid = ask_pos(l, r);
int GCD = 0;
if (l == 1 and r == n) {
for (int i = 2;i <= n;i++) GCD = gcd(GCD, abs(a[i] - a[i - 1]));
for (int i = 1; i * i <= GCD; i++) {
if (GCD % i) continue;
for (auto j : (i* i == GCD ? vector<int>{i} : vector<int>{ i, GCD / i })) {
if (j > a[mid] and j - a[mid] <= k) st.insert(j - a[mid]);
}
}
}
int g1 = dfs(dfs, l, mid - 1);
int g2 = dfs(dfs, mid + 1, r);
if (not(l == 1 and r == n)) {
GCD = gcd(g1, g2);
if (mid > l) GCD = gcd(GCD, a[mid] - a[mid - 1]);
if (mid + 1 <= r) GCD = gcd(GCD, a[mid + 1] - a[mid]);
nst.clear();
for (int i = 1; i * i <= GCD; i++) {
if (GCD % i) continue;
for (auto j : (i* i == GCD ? vector<int>{i} : vector<int>{ i, GCD / i })) {
for (auto j : st) {
if (GCD % (a[mid] + j) == 0) nst.insert(j);
}
}
}
st.swap(nst);
}
return GCD;
};
dfs(dfs, 1, n);
ll sum = 0;
for (auto i : st)
sum += i;
cout << st.size() << " " << sum << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin >> T;
while (T--) Solve();
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5620kb
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: 9776kb
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: 2ms
memory: 5680kb
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 78th lines differ - expected: '2 4', found: '0 0'