QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#614821 | #5045. King | ANIG# | TL | 351ms | 24808kb | C++14 | 1.4kb | 2024-10-05 16:59:16 | 2024-10-05 16:59:19 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int t,n,mods,f[N],p[N],res,dy[N],idx,cs;
int pows(int a,int b){
if(b==0)return 1;
int res=pows(a,b>>1);
res=res*res%mods;
if(b&1)res=res*a%mods;
return res;
}
map<int,int>q;
unordered_map<int,int>jl[N];
vector<int>g[N];
int fdl(int x,int y){
auto w=lower_bound(g[x].begin(),g[x].end(),y);
if(w==g[x].begin())return 0;
return *(--w);
}
int fdr(int x,int y){
auto w=upper_bound(g[x].begin(),g[x].end(),y);
if(w==g[x].end())return 0;
return *w;
}
void solve(int a,int b){
if(jl[a][b])return;
int k=p[b]*pows(p[a],mods-2)%mods,invk=pows(k,mods-2);
int nw=p[a],ans=2;
jl[a][b]=1;
while(1){
nw=nw*invk%mods;
if(!q.count(nw))break;
int lst=a;
a=fdl(q[nw],a);
cs++;
if(!a)break;
if(lst-a<=2)jl[a][lst]=1;
ans++;
}
nw=p[b];
while(1){
nw=nw*k%mods;
if(!q.count(nw))break;
int lst=b;
b=fdr(q[nw],b);
cs++;
if(!b)break;
if(b-lst<=2)jl[lst][b]=1;
ans++;
}
res=max(res,ans);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
res=0;
q.clear();idx=0;
cin>>n>>mods;
for(int i=1;i<=n;i++){
jl[i].clear();
cin>>p[i];
if(!q.count(p[i]))q[p[i]]=++idx;
g[q[p[i]]].push_back(i);
}
cs=0;
for(int i=1;i<n;i++){
solve(i,i+1);
if(i<n-1)solve(i,i+2);
}
if(res<n/2.0)cout<<"-1\n";
else cout<<res<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22568kb
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: 252ms
memory: 22560kb
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: 248ms
memory: 23492kb
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: 253ms
memory: 22920kb
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: 254ms
memory: 23892kb
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: 0
Accepted
time: 296ms
memory: 24800kb
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:
-1 -1 1976 -1 1443 -1 1206 1958 -1 1973 -1 -1 1294 -1 -1 -1 1223 -1 1457 1496 1860 1473 -1 -1 -1 -1 1654 -1 -1 -1 1505 1306 -1 -1 1696 -1 -1 -1 1475 1133 1189 1609 -1 -1 1397 1540 -1 1625 1191 -1 1923 -1 1835 -1 1296 -1 -1 -1 -1 1893 1705 1378 1343 -1 -1 1444 -1 -1 -1 1788 -1 1255 -1 -1 -1 -1 -1 -1 ...
result:
ok 100 numbers
Test #7:
score: 0
Accepted
time: 295ms
memory: 24000kb
input:
100 2000 275587717 83150209 104356116 34298444 197772460 101494007 261547554 223901747 141042340 131644199 60357915 86344257 81799664 202980147 152575387 115211945 7568397 260000358 100079566 95526927 11929407 165620994 44220242 62535336 42656523 39073826 149365602 253784018 119905129 214185578 2576...
output:
-1 1379 -1 -1 -1 1294 1717 -1 1569 1573 1375 -1 -1 1151 1859 1927 -1 1953 -1 1655 -1 -1 1986 -1 1019 -1 -1 1791 -1 1641 1783 1092 1488 1364 -1 1640 1875 1080 -1 1860 -1 1509 1578 -1 -1 1303 -1 1783 -1 -1 1744 1727 1525 -1 -1 1462 -1 -1 -1 -1 1316 1389 1144 -1 -1 1361 -1 1866 1334 -1 1313 1792 1286 -...
result:
ok 100 numbers
Test #8:
score: 0
Accepted
time: 351ms
memory: 24808kb
input:
100 2000 728295619 196905643 523233418 352318827 369770119 347085435 127560509 546671396 362387446 649410690 156802530 298823172 508289744 452232923 497191988 294603059 371099463 359230006 112535834 365805832 66877149 503870773 709803811 404046859 570062640 574727791 550224131 433734296 17881765 652...
output:
1982 -1 1859 1702 -1 1998 1377 -1 1731 1754 1498 1079 -1 1329 1879 -1 1334 -1 -1 1105 -1 -1 -1 1756 1165 -1 -1 -1 1328 -1 -1 1307 1815 -1 1666 1159 1263 -1 -1 -1 -1 -1 1443 -1 -1 1796 -1 1122 1310 1183 -1 -1 -1 -1 -1 1687 1122 1906 1200 -1 -1 1899 1543 -1 1487 -1 -1 -1 1885 1263 1325 -1 -1 1164 1548...
result:
ok 100 numbers
Test #9:
score: 0
Accepted
time: 291ms
memory: 24648kb
input:
100 2000 933493147 311022270 68427073 410339431 66474535 75931530 216168461 596520713 772600691 587660302 307741208 399247506 406645429 62857626 3532644 485034914 95405783 290695394 802486951 113550555 678338176 331200438 608959909 689318946 20753772 157370341 699828644 477646090 360131489 626962275...
output:
-1 1314 -1 -1 -1 1309 -1 1693 -1 1491 1438 -1 1012 1591 -1 -1 -1 1008 1917 1829 -1 1580 -1 1904 -1 1903 1155 1509 1378 1328 1050 -1 1576 -1 1752 1469 1858 -1 1261 -1 -1 1951 -1 1563 -1 1317 -1 1502 1867 -1 -1 1623 -1 -1 -1 1956 -1 -1 1215 1619 -1 -1 1852 1957 1561 -1 1493 -1 -1 1649 1110 1484 1429 1...
result:
ok 100 numbers
Test #10:
score: -100
Time Limit Exceeded
input:
10 20000 394172071 215340230 130633568 345392055 252111757 290095347 168672848 194628253 178431552 4977718 99966452 268841997 280806854 303621488 25038073 104682112 315461954 307022385 171592359 96110115 293022137 333987173 216781089 336833348 28521180 90994503 276664376 261103859 334641831 14856855...