QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#186422 | #6405. Barkley | ucup-team228# | WA | 1ms | 8276kb | C++20 | 2.3kb | 2023-09-23 20:00:58 | 2023-09-23 20:00:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
struct sparse_table{
static const int N = 1e5 + 10, L = 20;
T a[N];
int d[N][L], rem[N];
void build(int n) {
for (int i = 1; i <= n; i++) { d[i][0] = i; }
for (int j = 1; j <= L - 1; j++) {
for (int i = 1; i <= n; i++) {
if (i + (1 << j) - 1 > n) { continue; }
d[i][j] = __gcd(a[d[i][j - 1]], a[d[i + (1 << (j - 1))][j - 1]]);
}
}
for (int i = 1; i <= n; i++) { rem[i] = log2(i); }
}
T get(int l, int r) {
int lg = rem[r - l + 1];
return __gcd(a[d[l][lg]], a[d[r - (1 << lg) + 1][lg]]);
}
};
sparse_table<ll> table;
const int N = 1e5 + 10;
ll a[N];
vector<pair<int, ll>> rig[N];
ll solve(int l, int r, int k) {
if (k >= r - l + 1) {
return 1;
} else if (k == 0) {
return table.get(l, r);
} else {
ll ans = max(solve(l + 1, r, k - 1), solve(l, r - 1, k - 1));
for (auto [x, val] : rig[l]) {
ans = max(ans, __gcd(val, solve(x + 2, r, k - 1)));
}
return ans;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
table.a[i] = a[i];
}
table.build(n);
rig[n] = {{n, a[n]}};
for (int l = n - 1; l >= 1; l--) {
rig[l] = {{l, a[l]}};
for (auto [r, val] : rig[l + 1]) {
val = __gcd(val, a[l]);
if (val == rig[l].back().second) {
rig[l].back().first = r;
} else {
rig[l].emplace_back(r, val);
}
}
}
// for (int i = 1; i <= n; i++) {
// for (auto [r, val] : rig[i]) {
// cout << r << " " << val << "\n";
// }
// cout << "\n";
// }
while (q--) {
int l, r, k;
cin >> l >> r >> k;
cout << solve(l, r, k) << "\n";
}
#ifdef LOCAL
cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 8276kb
input:
4 4 3 2 6 4 1 3 1 2 4 1 1 4 2 1 4 3
output:
3 2 3 6
result:
ok 4 number(s): "3 2 3 6"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 8064kb
input:
100 10000 7 25 33 17 13 33 24 29 11 1 3 19 2 20 33 23 14 24 15 12 3 1 5 13 6 35 15 21 10 34 31 19 7 33 17 26 26 1 19 21 31 5 29 20 18 32 19 18 19 31 11 26 6 19 2 26 23 1 4 2 31 21 29 30 1 14 20 23 14 32 4 34 13 29 5 26 24 29 28 5 26 26 21 19 2 33 2 31 30 3 23 24 26 32 36 21 21 11 5 9 56 57 1 90 97 1...
output:
26 7 13 24 13 1 24 7 31 7 7 24 24 12 26 24 1 13 7 7 7 29 7 24 24 1 7 7 25 13 24 7 7 24 6 7 24 7 7 24 24 13 7 3 7 7 25 1 7 24 24 21 3 24 24 1 7 19 7 7 24 33 13 7 7 1 1 7 24 24 13 7 1 7 24 7 7 7 24 7 7 24 13 7 1 7 33 7 1 7 24 13 33 24 7 7 26 24 13 7 7 1 1 7 33 7 24 7 33 24 7 7 24 24 7 25 24 26 24 7 7 ...
result:
wrong answer 2nd numbers differ - expected: '1', found: '7'