QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#748877 | #9738. Make It Divisible | kans2298 | TL | 587ms | 9504kb | C++17 | 2.8kb | 2024-11-14 21:48:37 | 2024-11-14 21:48:38 |
Judging History
answer
#include<cstdio>
#include<iostream>
#include<set>
#include<algorithm>
#include<random>
#include<numeric>
#include<cmath>
#define N 50000
#define LIM 40000
using namespace std;
int b[N+5];
struct node
{
int pos,v;
};
node c[N+5];
int k;
void query_k(int a,int b,set<int> &f)
{
if (a<b) swap(a,b);
for (int x=1;x<=min(k,LIM);++x)
if ((a+x)%(b+x)==0) f.insert(x);
for (int y=2;y<=LIM;++y)
{
long long t=a-(long long)(y)*b,t2=t/(y-1);
if (t%(y-1)==0 && t2>=1 && t2<=k) f.insert(t2);
}
}
int st[N+5][17];
int query(int l,int r)
{
int x=floor(log2(r-l+1));
return gcd(st[l][x],st[r-(1<<x)+1][x]);
}
int main()
{
int t,ti;
// freopen("input.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>t;
for (ti=1;ti<=t;++ti)
{
int n,i,j;
cin>>n>>k;
for (i=1;i<=n;++i)
{
cin>>b[i];
c[i]={i,b[i]};
}
sort(c+1,c+n+1,[](const node &a,const node &b) -> bool {
return a.v<b.v;
});
if (n==1)
{
cout<<k<<" "<<((long long)(k+1))*(k)/2<<"\n";
continue;
}
set<int> kk,k2,res;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist1(1,n-1);
for (i=1;i<=3;++i)
{
int x=dist1(gen);
kk.clear();
query_k(b[x],b[x+1],kk);
// cout<<b[x]<<" "<<b[x+1]<<"\n";
if (i==1) res=kk;
else
{
k2=res;
res.clear();
set_intersection(kk.begin(),kk.end(),k2.begin(),k2.end(),inserter(res,res.begin()));
}
}
kk=res;
// cout<<"k=";
// for (auto x:kk)
// {
// cout<<x<<" ";
// }
// cout<<"\n";
vector<int> ans;
for (auto x:kk)
{
bool okay=true;
for (i=1;i<=n;++i)
st[i][0]=b[i]+x;
for (j=1;j<=16;++j)
for (i=1;i<=n;++i)
st[i][j]=gcd(st[i][j-1],st[min(i+(1<<(j-1)),n)][j-1]);
set<int> pp;
pp.insert(0);
pp.insert(n+1);
for (i=1;i<=n;++i)
{
set<int>::iterator it=pp.upper_bound(c[i].pos);
int l,r;
r=*it;
l=*prev(it);
l=l+1,r=r-1;
if (query(l,r)!=c[i].v+x)
{
okay=false;
break;
}
pp.insert(c[i].pos);
}
if (okay) ans.push_back(x);
}
cout<<ans.size()<<" ";
long long sum=0;
for (auto elm:ans)
sum+=elm;
cout<<sum<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5752kb
input:
3 5 10 7 79 1 7 1 2 1000000000 1 2 1 100 1000000000
output:
3 8 0 0 100 5050
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 3ms
memory: 3604kb
input:
4 201 1000000000 1 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5...
output:
0 0 0 0 0 0 0 0
result:
ok 4 lines
Test #3:
score: 0
Accepted
time: 587ms
memory: 9504kb
input:
500 4 1000000000 8 14 24 18 4 1000000000 17 10 18 14 4 1000000000 6 17 19 19 4 1000000000 15 14 15 25 4 1000000000 16 16 5 25 4 1000000000 4 30 20 5 4 1000000000 11 4 23 9 4 1000000000 14 25 13 2 4 1000000000 18 18 1 15 4 1000000000 22 22 22 28 4 1000000000 15 17 17 10 4 1000000000 22 14 13 25 4 100...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 500 lines
Test #4:
score: -100
Time Limit Exceeded
input:
1 50000 1000000000 230 286458 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 ...