QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#606079 | #9426. Relearn through Review | shift | WA | 176ms | 3856kb | C++20 | 2.4kb | 2024-10-02 21:59:20 | 2024-10-02 21:59:20 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
template<typename T>
struct RMQ {
int n;
std::vector<int> BIT;
std::vector<std::vector<T>> st;
RMQ() {}
RMQ(const std::vector<T> &v) {
init(v);
}
void initBIT(int n) {
BIT.resize(n + 1);
for(int i = 2; i <= n; i ++ ) {
BIT[i] = BIT[i / 2] + 1;
}
}
void init(const std::vector<T> &v) {
n = v.size();
initBIT(n);
const int K = BIT[n];
st.assign(n, std::vector<T>(K + 1));
for(int i = 0; i < n; i ++ ) {
st[i][0] = v[i];
}
for(int x = 1; x <= K; x ++ ) {
for(int i = 0; i + (1 << x) - 1 < n; i ++ ) {
st[i][x] = std::gcd(st[i][x - 1], st[i + (1 << (x - 1))][x - 1]);
}
}
}
T query(int l, int r) {
if(l == r) return 0;
int B = BIT[r - l + 1];
return std::gcd(st[l][B], st[r - (1 << B) + 1][B]);
}
};
void solve() {
i64 n, k;
std::cin >> n >> k;
std::vector<i64> g(n + 2), rg(n + 2), a(n + 2);
for(int i = 1; i <= n; i ++ ) {
std::cin >> a[i];
g[i] = std::gcd(g[i - 1], a[i]);
}
for(int i = n; i >= 1; i -- ) {
rg[i] = std::gcd(rg[i + 1], a[i]);
}
for(int i = 1; i <= n; i ++ ) {
a[i] += k;
}
RMQ<i64> rmq(a);
i64 ans = g[n];
for(int i = 1; i <= n; i ++ ) {
int j = i + 1;
while(j < n and g[j] == g[i]) j ++;
if(j == n) break;
for(int r = n; r > j; r -- ) {
int l = r - 1;
while(l > j and std::gcd(rg[l], g[i]) == std::gcd(rg[r], g[i])) l --;
ans = std::max(ans, std::gcd(std::gcd(g[i], rg[r]), rmq.query(j, l)));
r = l + 1;
}
i = j - 1;
}
for(int i = 1; i <= n; i ++ ) {
ans = std::max(ans, std::gcd(g[i - 1], rmq.query(i, n)));
}
for(int i = n; i >= 1; i -- ) {
ans = std::max(ans, std::gcd(rg[i + 1], rmq.query(1, i)));
}
std::cout << ans << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t = 1;
std::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: 3600kb
input:
2 6 2 5 3 13 8 10 555 3 0 3 6 9
output:
5 3
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 176ms
memory: 3856kb
input:
100000 1 608611451460421713 33155506392034032 1 743116173559300609 6138108577573005 7 364454564010802125 657035115675878115 657035115675878115 657035115675878115 657035115675878115 657035115675878115 292580551665075990 657035115675878115 4 316648374341335221 365788422120542814 182894211060271407 731...
output:
33155506392034032 6138108577573005 657035115675878115 182894211060271407 880411769063535667 98423435849394582 183698346865682381 962990836390050009 484915690810412536 878097339332572161 149180825015886938 997057718507559252 915781395066183375 37337367838628559 632093288650732211 754243427814661856 3...
result:
wrong answer 1st lines differ - expected: '641766957852455745', found: '33155506392034032'