QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#302417 | #5045. King | LittleXi# | WA | 88ms | 3760kb | C++20 | 3.6kb | 2024-01-10 20:50:26 | 2024-01-10 20:50:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll qpow(ll a, ll b, ll p)
{
ll ret = 1;
while(b)
{
if(b & 1) ret = ret * a % p;
a = a * a % p;
b >>= 1;
}
return ret;
}
int Lower(const vector<int>& vec,int x)
{
int Left=-1,Right=vec.size();
while (Left+1<Right)
{
int Mid=Left+Right>>1;
if (vec[Mid]>=x) Right=Mid;
else Left=Mid;
}
return Right;
}
int Upper(const vector<int>& vec,int x)
{
int Left=-1,Right=vec.size();
while (Left+1<Right)
{
int Mid=Left+Right>>1;
if (vec[Mid]>x) Right=Mid;
else Left=Mid;
}
return Right;
}
//(i-d, i)
int check(int i, int d, ll p, vector<ll>& b, const vector<ll>& binv, unordered_map<ll,vector<int>>& pos)
{
//printf("check(%d, %d)\n",i,d);
ll q = b[i] * binv[i-d] % p;
ll qinv = binv[i] * b[i-d] % p;
int nowans = 2;
//jump right
int nxtpos = i+1; ll nxtval = b[i] * q % p;
while(1)
{
auto it = pos.find(nxtval);
if(it == pos.end()) break;
auto& vec = it->second;
// auto jt = lower_bound(vec.begin(), vec.end(), nxtpos);
int jt=Lower(vec,nxtpos);
if (jt==vec.size()) break;
// if(jt == vec.end()) break;
//printf("jump right to %d\n",*jt);
++nowans;
// nxtpos = (*jt)+1;
nxtpos=vec[jt]+1;
nxtval = nxtval * q % p;
}
//jump left
nxtpos = i-d-1; nxtval = b[i-d] * qinv % p;
while(1)
{
auto it = pos.find(nxtval);
if(it == pos.end()) break;
auto& vec = it->second;
// auto jt = upper_bound(vec.begin(), vec.end(), nxtpos);
int jt=Upper(vec,nxtpos);
// if(jt == vec.begin()) break;
if (jt<=0) break;
--jt; ++nowans;
//printf("jump left to %d\n",*jt);
// nxtpos = (*jt)-1;
nxtpos=vec[jt]-1;
nxtval = nxtval * qinv % p;
}
//printf("i=%d, d=%d, q=%d, nowans=%d\n",i,d,q,nowans);
return nowans;
}
void solve()
{
mt19937_64 rnd(98275314);
int n; ll p; scanf("%d%lld",&n,&p);
vector<ll> b(n+1), binv(n+1);
for(int i = 1; i <= n; ++i) scanf("%lld",&b[i]), binv[i] = qpow(b[i], p-2, p);
int ans = 2;
if(n == 2)
{
printf("2\n");
return;
}
//check 1,3,5,...
if(n >= 3)
{
ll q = b[3] * binv[1] % p; bool flag = true;
for(int i = 3; i <= n; i += 2)
{
if(b[i] * binv[i-2] % p != q)
{
flag = false;
break;
}
}
if(flag) ans = max(ans, (n+1)/2);
}
//check 2,4,6,...
if(n % 2 == 0 && n >= 4)
{
ll q = b[4] * binv[2] % p; bool flag = true;
for(int i = 4; i <= n; i += 2)
{
if(b[i] * binv[i-2] % p != q)
{
flag = false;
break;
}
}
if(flag) ans = max(ans, n/2);
}
unordered_map<ll,vector<int>> pos;
for(int i = 1; i <= n; ++i) pos[b[i]].push_back(i);
for(int t = 1; t <= 10; ++t)
{
int i = abs((ll)rnd()) % (n-1) + 2;
ans = max(ans, check(i, 1, p, b, binv, pos));
}
for(int t = 1; t <= 10; ++t)
{
int i = abs((ll)rnd()) % (n-2) + 3;
ans = max(ans, check(i, 2, p, b, binv, pos));
}
if(2*ans < n) printf("-1\n");
else printf("%d\n",ans);
}
int main()
{
int T; scanf("%d",&T);
while(T--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3744kb
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: -100
Wrong Answer
time: 88ms
memory: 3760kb
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 -1 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 12...
result:
wrong answer 10th numbers differ - expected: '108', found: '-1'