QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#625978 | #9426. Relearn through Review | ORZZZ | WA | 1ms | 5624kb | C++14 | 1.7kb | 2024-10-09 22:08:10 | 2024-10-09 22:08:11 |
Judging History
answer
#include<bits/stdc++.h>
#define endl '\n'
#define gcd __gcd
#define int long long
using namespace std;
const int N = 3e5 + 10;
int a[N], g[N][20];
int get(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return gcd(g[l][k], g[r - (1 << k) + 1][k]);
}
int gcd_3(int a, int b, int c){
return gcd(a, gcd(b, c));
}
void solve(){
int n, k; cin >> n >> k;
int ans = 0;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
g[i][0] = a[i] + k;
ans = gcd(a[i], ans);
}
int pre[n + 5] = {}, suf[n + 5] = {};
for(int i = 1; i <= n; ++i) pre[i] = gcd(pre[i - 1], a[i]);
for(int i = n; i >= 1; --i) suf[i] = gcd(suf[i + 1], a[i]);
for(int j = 1; j <= 20; ++j){
for(int i = 1; i + (1 << j) - 1 <= n; ++i){
g[i][j] = gcd(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]);
}
}
for(int r = 1; r <= n; ++r){
ans = max(ans, gcd(get(1, r), suf[r + 1]));
}
if(n <= 1e3)
{
for(int l = 1; l <= n; ++l){
for(int r = l; r <= n; ++r) ans = max(ans, gcd_3(pre[l - 1], get(l, r), suf[r + 1]));
}
return;
}
for(int l = 2; l <= n; ++l){
int lo = l, hi = n, r = n;
while(lo <= hi){
int mid = (lo + hi) / 2;
if(suf[mid + 1] % pre[l - 1]){
lo = mid + 1;
}
else{
r = mid;
hi = mid - 1;
}
}
if(r != n) ans = max(ans, gcd_3(pre[l - 1], suf[r + 1], get(l, r)));
else{
for(int R = n; R >= max(l, R - 100); --R)
ans = max(ans, gcd_3(pre[l - 1], suf[R + 1], get(l, R)));
for(int R = l; R <= min(l + 100, n); ++R)
ans = max(ans, gcd_3(pre[l - 1], suf[R + 1], get(l, R)));
}
}
cout << ans << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
cin >> T;
while(T--) solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5624kb
input:
2 6 2 5 3 13 8 10 555 3 0 3 6 9
output:
result:
wrong answer 1st lines differ - expected: '5', found: ''