QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#748944 | #9738. Make It Divisible | kans2298 | TL | 0ms | 0kb | C++17 | 3.0kb | 2024-11-14 22:05:32 | 2024-11-14 22:05:32 |
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<=k;++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;
set<int> nn;
for (i=1;i<=n;++i)
{
cin>>b[i];
c[i]={i,b[i]};
nn.insert(b[i]);
}
sort(c+1,c+n+1,[](const node &a,const node &b) -> bool {
return a.v<b.v;
});
if (nn.size()==1)
{
cout<<k<<" "<<((long long)(k+1))*(k)/2<<"\n";
continue;
}
set<int> kk,k2,res;
random_device rd;
mt19937 gen(rd());
vector<int> unx;
for (auto elm:nn)
unx.push_back(elm);
uniform_int_distribution<> dist1(0,unx.size()-2);
for (i=1;i<=3;++i)
{
int x=dist1(gen);
kk.clear();
query_k(unx[x],unx[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;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
3 5 10 7 79 1 7 1 2 1000000000 1 2 1 100 1000000000