QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#797250 | #9426. Relearn through Review | lengli | TL | 205ms | 3672kb | C++23 | 1.5kb | 2024-12-02 19:51:44 | 2024-12-02 19:51:45 |
Judging History
answer
/*
lengli_QAQ
Hope there are no bugs!!!
*/
#include <bits/stdc++.h>
#define fastio std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0)
#define all(x) x.begin(),x.end()
#define pb push_back
#define i64 long long
struct ST{
std::vector<std::vector<i64>> f;
int n=0,m=0;
ST(){};
ST(int nn,std::vector<i64> &a,i64 k){
n=nn,m=std::__lg(nn)+1;
f.resize(n+1,std::vector<i64> (m+1));
for(int len=0;len<m;len++){
for(int i=1;i+(1<<len)-1<=n;i++){
if(!len) f[i][len]=a[i]+k;
else f[i][len]=std::gcd(f[i][len-1],f[i+(1<<len-1)][len-1]);
}
}
}
i64 query(int l,int r){
int k=std::__lg(r-l+1);
return std::gcd(f[l][k],f[r-(1<<k)+1][k]);
}
};
void solve(){
i64 n,k;
std::cin>>n>>k;
std::vector<i64> a(n+2),pre(n+2,0),suf(n+2,0);
for(int i=1;i<=n;i++) std::cin>>a[i];
for(int i=1;i<=n;i++) pre[i]=std::gcd(pre[i-1],a[i]);
for(int i=n;i>=1;i--) suf[i]=std::gcd(suf[i+1],a[i]);
i64 res=pre[n];
std::vector<int> q;
ST st(n,a,k);
i64 la=0;
for(int i=1;i<=n;i++){
la=std::gcd(pre[i-1],a[i]+k);
if(la!=pre[i]) q.pb(i);
for(auto j:q){
i64 ans=st.query(j,i);
ans=std::gcd(suf[i+1],ans);
ans=std::gcd(pre[j-1],ans);
res=std::max(res,ans);
}
}
std::cout<<res<<"\n";
}
signed main(){
fastio;
int T;
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: 3548kb
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: 0
Accepted
time: 205ms
memory: 3672kb
input:
100000 1 608611451460421713 33155506392034032 1 743116173559300609 6138108577573005 7 364454564010802125 657035115675878115 657035115675878115 657035115675878115 657035115675878115 657035115675878115 292580551665075990 657035115675878115 4 316648374341335221 365788422120542814 182894211060271407 731...
output:
641766957852455745 749254282136873614 657035115675878115 182894211060271407 880411769063535667 560553564512176618 183698346865682381 962990836390050009 616597869896951268 878097339332572161 188820994675344528 997057718507559252 949074379610491450 37337367838628559 632093288650732211 3771217139073309...
result:
ok 100000 lines
Test #3:
score: -100
Time Limit Exceeded
input:
1000 71 451750502977198411 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 701513700102652904 7015137...
output:
701513700102652904 628264251002959880 866034990978685601 718723820869997225 525309136656747615 453291245761058554 420366973911241294 500173849665919725 16701821680586640 794711320668492112 799961738480944637 963500289005941882 190368877908873112 973069943210898565 629019279628092667 1921616220783983...