QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625342 | #9426. Relearn through Review | Idtwtei | ML | 0ms | 9984kb | C++14 | 1.3kb | 2024-10-09 18:47:19 | 2024-10-09 18:47:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
const int N=3e5+100,INF=1e18;
#define gc getchar()
#define rd read()
inline int read(){
int x=0,f=0; char c=gc;
for(;c<'0'||c>'9';c=gc) f|=(c=='-');
for(;c>='0'&&c<='9';c=gc) x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
int n,k,ans,a[N],pre[N],nxt[N];
vector<int> cl,cr;
#define lc (id<<1)
#define rc (id<<1|1)
#define mid (l+r>>1)
int sum[N<<2];
inline void psu(int id){ sum[id]=__gcd(sum[lc],sum[rc]); }
void bui(int id,int l,int r){
if(l==r) return sum[id]=a[l]+k,void();
bui(lc,l,mid),bui(rc,mid+1,r),psu(id);
}
int ask(int id,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return sum[id]; int res=0;
if(ql<=mid) res=__gcd(res,ask(lc,l,mid,ql,qr));
if(qr>=mid+1) res=__gcd(res,ask(rc,mid+1,r,ql,qr));
return res;
}
void solve(){
n=rd,k=rd; for(int i=1;i<=n;++i) a[i]=rd;
pre[0]=0; for(int i=1;i<=n;++i) pre[i]=__gcd(pre[i-1],a[i]);
nxt[n+1]=0; for(int i=n;i;--i) nxt[i]=__gcd(nxt[i+1],a[i]);
ans=pre[n],bui(1,1,n);
for(int i=1;i<=n;++i) if(pre[i-1]!=pre[i]) cl.pb(i);
for(int i=1;i<=n;++i) if(nxt[i+1]!=nxt[i]) cr.pb(i);
for(int l:cl) for(int r:cr) if(l<=r) ans=max(ans,__gcd(pre[l-1],__gcd(ask(1,1,n,l,r),nxt[r+1])));
printf("%lld\n", ans);
}
signed main(){
int T=rd;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9984kb
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
Memory Limit Exceeded
input:
100000 1 608611451460421713 33155506392034032 1 743116173559300609 6138108577573005 7 364454564010802125 657035115675878115 657035115675878115 657035115675878115 657035115675878115 657035115675878115 292580551665075990 657035115675878115 4 316648374341335221 365788422120542814 182894211060271407 731...