QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#370502#5045. Kingzzuqy#TL 1449ms9180kbC++112.2kb2024-03-29 09:46:452024-03-29 09:46:46

Judging History

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

  • [2024-03-29 09:46:46]
  • 评测
  • 测评结果:TL
  • 用时:1449ms
  • 内存:9180kb
  • [2024-03-29 09:46:45]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
long long read()
{
    long long x;scanf("%lld",&x);return x;
}
ll quick(ll a,ll b,ll mod)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=ans*a%mod;
        b=b/2;
        a=a*a%mod;
    }
    return ans;
}
ll a[200010],n,p;
map<int,int>id;//表示
vector<int>pos[200010];
int ask1()
{
    int x=(1ll*rand()*rand()+rand())%n+1;
    while(x==n)
        x=(1ll*rand()*rand()+rand())%n+1;
    int y=min(n,x+1ll);
    int q=a[y]*quick(a[x],p-2,p)%p;
    ll qq=quick(q,p-2,p);
    int len=2;
    while(x!=1)//往前跳
    {
        ll v=id[a[x]*qq%p];
        if(v==0||pos[v][0]>=x)
            break;
        len++;
        x=*(lower_bound(pos[v].begin(),pos[v].end(),x)-1);

    }
    while(y!=n)
    {
        ll v=id[a[y]*q%p];
        if(v==0||pos[v].back()<=y)
            break;
        len++;
        y=*upper_bound(pos[v].begin(),pos[v].end(),y);
    }
    return len;
}
int ask2()
{
    int x=(1ll*rand()*rand()+rand())%n+1;
    while(x==n)
        x=(1ll*rand()*rand()+rand())%n+1;
    int y=min(n,x+2ll);
    int q=a[y]*quick(a[x],p-2,p)%p;//
    int qq=quick(q,p-2,p);
    int len=2;
    while(x!=1)//往前跳
    {
        ll v=id[a[x]*qq%p];
        if(v==0||pos[v][0]>=x)
            break;
        len++;
        x=*(lower_bound(pos[v].begin(),pos[v].end(),x)-1);

    }
    while(y!=n)
    {
        ll v=id[a[y]*q%p];
        if(v==0||pos[v].back()<=y)
            break;
        len++;
        y=*upper_bound(pos[v].begin(),pos[v].end(),y);
    }
    return len;
}
void work()
{
    n=read();p=read();
    id.clear();
    int sum=0;
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    for(int i=1;i<=n;i++)
    {
        if(id[a[i]]==0)
        {
            sum++;
            id[a[i]]=sum;
            pos[sum].push_back(i);
        }
    }
    int ans=-1;
    for(int i=1;i<=500;i++)
        ans=max(ans,ask1()),ans=max(ans,ask2());
    if(ans*2<n)
        ans=-1;
    cout<<ans<<'\n';
    for(int i=1;i<=sum;i++)
        pos[i].clear();
}
signed main()
{
    srand(time(0));
    for(int t=read();t;t--)
        work();
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 8864kb

input:

4
6 1000000007
1 1 2 4 8 16
6 1000000007
597337906 816043578 617563954 668607211 89163513 464203601
5 1000000007
2 4 5 6 8
5 1000000007
2 4 5 6 7

output:

5
-1
3
-1

result:

ok 4 number(s): "5 -1 3 -1"

Test #2:

score: 0
Accepted
time: 1428ms
memory: 8416kb

input:

1000
200 495189361
193302375 262009153 248101278 250233641 303504256 426913173 23261177 206011896 214770731 286184509 492688635 207979481 282629026 450810670 41818047 359796006 445343921 241742611 249404909 41291916 392252331 125287519 92825425 162555413 371172157 420486666 270651384 309213995 11709...

output:

-1
133
-1
187
163
114
114
132
100
108
116
137
115
-1
165
115
142
165
-1
-1
108
129
-1
144
122
-1
111
146
190
159
134
-1
117
180
196
-1
-1
192
123
110
-1
106
153
103
-1
-1
103
169
-1
174
152
-1
181
113
135
-1
153
199
182
-1
177
152
177
162
-1
115
-1
-1
-1
113
128
-1
-1
-1
-1
168
167
-1
140
-1
-1
-1
1...

result:

ok 1000 numbers

Test #3:

score: 0
Accepted
time: 1446ms
memory: 9180kb

input:

1000
200 949663931
479217281 320985989 765005251 6434894 250659962 96381980 929176627 859155083 837781618 895381960 276913403 206799800 594561194 451400141 287326771 230131436 499694353 149062037 769076212 249680039 876506380 680573243 672238085 235553233 593493391 524937617 832401231 490042272 4494...

output:

198
-1
-1
-1
120
-1
162
-1
101
-1
103
157
188
-1
-1
177
-1
126
114
-1
177
-1
-1
150
-1
148
140
165
-1
102
-1
192
-1
-1
-1
181
149
196
-1
-1
173
147
-1
149
-1
-1
200
-1
-1
-1
-1
-1
156
-1
114
119
199
127
123
138
169
188
146
-1
137
-1
-1
-1
121
-1
-1
121
-1
-1
-1
108
117
127
-1
127
194
-1
121
-1
-1
12...

result:

ok 1000 numbers

Test #4:

score: 0
Accepted
time: 1440ms
memory: 8640kb

input:

1000
200 752331551
23573436 686888652 118880475 606109605 504896777 156156511 407092389 168008046 588489051 384747939 312334251 10179640 389706827 496937297 625716990 602916042 580060754 535624296 196865310 693079720 672788885 104900220 416739470 142969723 655913476 432092835 454858443 698545241 763...

output:

-1
179
-1
-1
126
-1
189
-1
115
-1
-1
-1
175
-1
109
-1
166
190
129
-1
-1
108
-1
135
-1
174
109
-1
183
-1
-1
-1
142
-1
105
159
163
144
-1
-1
-1
124
175
135
183
132
-1
-1
115
-1
-1
130
159
-1
-1
160
138
145
196
-1
123
163
-1
-1
181
-1
137
126
111
-1
-1
110
111
148
-1
-1
131
-1
-1
-1
101
-1
-1
-1
-1
-1
...

result:

ok 1000 numbers

Test #5:

score: 0
Accepted
time: 1449ms
memory: 8880kb

input:

1000
200 952479163
404962930 635613728 875512822 126836765 893278336 93755885 802367656 41132902 703323879 109154481 793288802 49973196 534490899 462994291 829278242 789093683 134072789 384627711 513380294 28212590 922131395 333371011 352605762 351655730 28129292 473972568 10559174 951685030 7877219...

output:

-1
-1
-1
171
-1
190
-1
-1
-1
107
-1
-1
176
-1
-1
118
-1
-1
119
-1
-1
160
-1
100
-1
-1
129
190
165
-1
-1
108
-1
-1
-1
188
167
121
189
190
113
153
-1
-1
-1
-1
142
192
186
102
108
-1
-1
-1
-1
-1
137
160
143
146
163
156
163
176
135
-1
-1
-1
-1
-1
-1
-1
-1
117
-1
-1
-1
147
166
-1
200
195
189
177
-1
143
-...

result:

ok 1000 numbers

Test #6:

score: -100
Time Limit Exceeded

input:

100
2000 146158349
19643059 687117 30976705 141997757 126939439 27291348 99640815 77869374 140943896 108863607 47555226 24332054 20814180 9774186 95917237 54599801 123926643 144812251 18602850 105663323 135089587 86540799 102680207 83257587 98590031 102391932 51059987 8865754 980981 32147720 8025236...

output:


result: