QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#614764#5045. KingANIG#TL 0ms0kbC++141.3kb2024-10-05 16:54:462024-10-05 16:54:50

Judging History

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

  • [2024-10-05 16:54:50]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-05 16:54:46]
  • 提交

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(){
	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);
		}
		cs=0;
		while(1){
			int i=rand()*rand()%(n-1)+1;
		    solve(i,i+1);
		    if(i<n-1)solve(i,i+2);
		    if(cs>n*25&&res>n/2.0)break;
		}
		if(res<n/2.0)cout<<"-1\n";
		else cout<<res<<"\n";
	}
}

详细

Test #1:

score: 0
Time Limit Exceeded

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:


result: