QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#516686 | #6405. Barkley | huayucaiji | WA | 3ms | 5940kb | C++14 | 2.5kb | 2024-08-12 20:33:36 | 2024-08-12 20:33:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int read() {
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9') {
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
char read_char() {
char ch=getchar();
while(!isalpha(ch)) {
ch=getchar();
}
return ch;
}
const int MAXN=1e5+10;
int n,q;
int a[MAXN],f[MAXN][21],p[MAXN][21],tot[MAXN];
int gcd(int x,int y) {
return y? gcd(y,x%y):x;
}
void init() {
for(int j=1;j<=20;j++) {
for(int i=1;i+(1<<j)-1<=n;i++) {
f[i][j]=gcd(f[i][j-1],f[i+(1<<j-1)][j-1]);
}
}
}
int getgcd(int l,int r) {
int k=log2(r-l+1);
return gcd(f[l][k],f[r-(1<<k)+1][k]);
}
int solve1(int l,int r,int g) {
if(r-l+1<=1) {
return 0;
}
int ans=gcd(g,getgcd(l,r-1));
ans=max(ans,gcd(g,getgcd(l+1,r)));
for(int i=1;i<=tot[l];i++) {
if(p[l][i]+2>r) {
break;
}
ans=max(ans,gcd(g,gcd(getgcd(l,p[l][i]),getgcd(p[l][i]+2,r))));
}
return ans;
}
int solve2(int l,int r,int g) {
if(r-l+1<=2) {
return 0;
}
int ans=gcd(g,solve1(l,r-1,g));
ans=max(ans,gcd(g,solve1(l+1,r,g)));
for(int i=1;i<=tot[l];i++) {
if(p[l][i]+2>r) {
break;
}
ans=max(ans,gcd(g,solve1(p[l][i]+2,r,getgcd(l,p[l][i]))));
}
return ans;
}
int solve3(int l,int r,int g) {
if(r-l+1<=3) {
return 0;
}
int ans=gcd(g,solve2(l,r-1,g));
ans=max(ans,gcd(g,solve2(l+1,r,g)));
for(int i=1;i<=tot[l];i++) {
if(p[l][i]+2>r) {
break;
}
ans=max(ans,gcd(g,solve2(p[l][i]+2,r,getgcd(l,p[l][i]))));
}
return ans;
}
int main() {
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++) {
f[i][0]=a[i]=read();
}
init();
for(int i=1;i<=n;i++) {
p[i][0]=i-1;
while(p[i][tot[i]]<n) {
int l=p[i][tot[i]]+1;
int r=n;
int g=getgcd(i,l);
while(l+1<r) {
int mid=(l+r)>>1;
if(getgcd(i,mid)==g) {
l=mid;
}
else {
r=mid-1;
}
}
if(getgcd(i,r)==g)
p[i][++tot[i]]=r;
else
p[i][++tot[i]]=l;
}
}
for(int i=1;i<=q;i++) {
int l,r,k;
l=read();r=read();k=read();
if(i==58665) {
cout<<l<< " "<<r<<" "<<k<<endl;
for(int j=l;j<=r;j++) {
cout<<a[j]<<" ";
}
puts("");
}
if(n>100) {
continue;
}
if(k==1) {
printf("%d\n",solve1(l,r,0));
}
if(k==2) {
printf("%d\n",solve2(l,r,0));
}
if(k==3) {
printf("%d\n",solve3(l,r,0));
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
/*
4 1
20 33 23 14
1 4 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5940kb
input:
4 4 3 2 6 4 1 3 1 2 4 1 1 4 2 1 4 3
output:
3 2 3 6
result:
ok 4 number(s): "3 2 3 6"
Test #2:
score: 0
Accepted
time: 2ms
memory: 5860kb
input:
100 10000 7 25 33 17 13 33 24 29 11 1 3 19 2 20 33 23 14 24 15 12 3 1 5 13 6 35 15 21 10 34 31 19 7 33 17 26 26 1 19 21 31 5 29 20 18 32 19 18 19 31 11 26 6 19 2 26 23 1 4 2 31 21 29 30 1 14 20 23 14 32 4 34 13 29 5 26 24 29 28 5 26 26 21 19 2 33 2 31 30 3 23 24 26 32 36 21 21 11 5 9 56 57 1 90 97 1...
output:
26 1 1 1 1 1 1 1 31 1 1 1 1 1 26 1 1 1 1 1 1 29 1 1 1 1 1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 21 1 1 1 1 1 19 1 1 1 21 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 3 1 2 1 26 1 1 1 1 1 1 1 7 1 1 1 33 1 1 1 1 1 1 2 1 26 1 1 1 2 1 1 1 1 1 1 26 1 1 1 1 31 1 1 2 1 4 29 1 2 1 1...
result:
ok 10000 numbers
Test #3:
score: -100
Wrong Answer
time: 3ms
memory: 5896kb
input:
1000 66666 25 21 18 19 9 34 16 7 36 2 8 10 25 15 34 9 1 34 6 19 20 20 1 16 10 6 10 1 30 34 6 15 15 11 9 4 34 36 27 17 2 2 19 10 4 22 15 16 22 36 27 26 20 23 29 16 27 14 3 31 32 16 5 5 31 13 27 5 17 23 20 19 13 22 30 14 25 13 7 16 10 6 1 6 3 5 36 1 33 22 31 26 28 3 14 14 2 31 35 7 19 30 36 5 3 14 14 ...
output:
816 820 3 33 1 6 14 24
result:
wrong answer 1st numbers differ - expected: '1', found: '816'