QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#614879#5045. KingANIG#TL 3ms11988kbC++141.5kb2024-10-05 17:05:142024-10-05 17:05:17

Judging History

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

  • [2024-10-05 17:05:17]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:11988kb
  • [2024-10-05 17:05:14]
  • 提交

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;
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){
	int k=p[b]*pows(p[a],mods-2)%mods,invk=pows(k,mods-2);
	int nw=p[a],ans=2;
	while(1){
		nw=nw*invk%mods;
		if(!q.count(nw))break;
		a=fdl(q[nw],a);
		if(!a)break;
		ans++;
	}
	nw=p[b];
	while(1){
		nw=nw*k%mods;
		if(!q.count(nw))break;
		b=fdr(q[nw],b);
		if(!b)break;
		ans++;
	}
	res=max(res,ans);
	cs+=ans;
}
signed main(){
	srand(time(0));
	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++){
			cin>>p[i];
			if(!q.count(p[i]))q[p[i]]=++idx;
			g[q[p[i]]].push_back(i);
		}
		auto stt=clock();
		cs=0;
		if(n>100)
		while(1){
			int i=rand()*rand()%(n-1)+1;
		    solve(i,i+1);
		    if(i<n-1)solve(i,i+2);
			if((clock()-stt)*1.0/CLOCKS_PER_SEC>1.8*n/200000.0)break;
		}
		else{
			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";
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 11988kb

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

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:


result: