QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#643641 | #9426. Relearn through Review | ray | WA | 150ms | 3644kb | C++20 | 2.0kb | 2024-10-15 22:35:02 | 2024-10-15 22:35:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
inline int read()
{
int num=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') num=(num<<1)+(num<<3)+ch-'0',ch=getchar();
return num*w;
}
const int N=3e5+10;
int n,k,maxn;//f[i][0/1][0/1]:前i个当前不加/加k,之前没用过/用过加
int gcd(int a, int b)
{
return __gcd(a,b);
if(a == 0) return b;
if(b == 0) return a;
if(!(a & 1) && !(b & 1)) // a % 2 == 0 && b % 2 == 0;
return gcd(a >> 1, b >> 1) << 1;
else if(!(b & 1))
return gcd(a, b >> 1);
else if(!(a & 1))
return gcd(a >> 1, b);
else
return gcd(abs(a - b), min(a, b));
}
template <typename T>
T max(T&& a, T&& b) {
return a < b ? b : a;
}
void solve()
{
n=read(),k=read(),maxn=0;
vector<int> a(n+1),g(n+1);
vector<vector<vector<int>>> f(n+1, vector<vector<int>>(2, vector<int>(2)));
for(int i=1;i<=n;i++) a[i]=read();
f[1][0][0]=a[1],f[1][1][0]=a[1]+k,g[n]=a[n];
if(n==1)
{
cout<<a[1]+k<<"\n";
return ;
}
for(int i=n-1;i>=1;i--) g[i]=gcd(a[i],g[i+1]);
for(int i=2;i<=n;i++)
{
f[i][0][0]=gcd(f[i-1][0][0],a[i]);
f[i][0][1]=max(gcd(f[i-1][1][0],a[i]),gcd(f[i-1][1][1],a[i]));
f[i][1][0]=gcd(f[i-1][0][0],a[i]+k);
if(i>2) f[i][1][1]=max(gcd(f[i-1][1][0],a[i]+k),gcd(f[i-1][1][1],a[i]+k));
else f[i][1][1]=gcd(f[i-1][1][0],a[i]+k);
if(i<n) maxn=max(maxn,gcd(f[i][0][1],g[i+1])),maxn=max(maxn,gcd(f[i][1][0],g[i+1]));
//else maxn=max(maxn,f[i][0][1]),maxn=max(maxn,f[i][1][0]);
}
maxn=max(maxn,f[n][1][1]),maxn=max(maxn,f[n][0][0]);
maxn=max(maxn,f[n][0][1]),maxn=max(maxn,f[n][1][0]);
cout<<maxn<<"\n";
}
signed main()
{
cin.tie(0)->sync_with_stdio(false);
int T=read();
while(T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3572kb
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: 150ms
memory: 3644kb
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 7542434278146618...
result:
wrong answer 16th lines differ - expected: '377121713907330928', found: '754243427814661856'