QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#748984#9738. Make It Divisiblekans2298TL 26ms5548kbC++173.7kb2024-11-14 22:15:062024-11-14 22:15:07

Judging History

你现在查看的是最新测评结果

  • [2024-11-27 18:44:44]
  • hack成功,自动添加数据
  • (/hack/1263)
  • [2024-11-14 22:15:07]
  • 评测
  • 测评结果:TL
  • 用时:26ms
  • 内存:5548kb
  • [2024-11-14 22:15:06]
  • 提交

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;
     set<pair<int,int>> nn;
     for (i=1;i<=n;++i)
          {
          cin>>b[i];
          c[i]={i,b[i]};
          }
     for (i=1;i<n;++i)
          if (b[i]<b[i+1]) nn.insert({b[i],b[i+1]});
               else if (b[i]>b[i+1]) nn.insert({b[i+1],b[i]});
     sort(c+1,c+n+1,[](const node &a,const node &b) -> bool {
          return a.v<b.v;
     });
     if (n==1 || nn.empty())
          {
          cout<<k<<" "<<((long long)(k+1))*(k)/2<<"\n";
          continue;
          }
     set<int> kk,k2,res;
     random_device rd;
     mt19937 gen(rd());
     vector<pair<int,int>> unx;
     for (auto elm:nn)
          unx.push_back(elm);
     uniform_int_distribution<> dist1(0,unx.size()-1);
     if (unx.size()<=10)
          {
          for (i=1;i<=5;++i)
          {
          kk.clear();
          query_k(unx[i-1].first,unx[i-1].second,kk);
          // cout<<unx[x].first<<" "<<unx[x].second<<"\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()));
               }
          }
          }
     else
     {
     for (i=1;i<=5;++i)
          {
          int x=dist1(gen);
          kk.clear();
          query_k(unx[x].first,unx[x].second,kk);
          // cout<<unx[x].first<<" "<<unx[x].second<<"\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: 11ms
memory: 5432kb

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: 26ms
memory: 5548kb

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: -100
Time Limit Exceeded

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:


result: