首页 > 其他 > 详细

【哈希表模板(链前版本)】【ybtoj】【特殊序列】

时间:2021-09-17 15:08:59      阅读:47      评论:0      收藏:0      [点我收藏+]

题意

根据一个递推式不断求出元素,判断这个数出没出现过。

题解

以前只写过邻接表的哈希表,没写过链前的哈希表,贴一下代码。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 0x3f3f3f3f,N = 2e6+10,mod = 1e6+7;
inline ll read()
{
	ll ret=0;char ch=‘ ‘,c=getchar();
	while(!(c>=‘0‘&&c<=‘9‘)) ch=c,c=getchar();
	while(c>=‘0‘&&c<=‘9‘) ret=(ret<<1)+(ret<<3)+c-‘0‘,c=getchar();
	return ch==‘-‘?-ret:ret;
}
struct edge
{
	int nxt,to;
}e[N];

ll a,b,c; 
ll x[N];
ll mp[N];
int ecnt=-1,cnt,head[N]; 
inline void add(int x,int y)
{
	e[++ecnt]=(edge){head[x],y};
	head[x]=ecnt;
}
inline void insert(int x)
{
	int u=x%mod;
	mp[++cnt]=x;
	add(u,cnt);
}
inline bool find(int x)
{
	int u=x%mod;
	for(int i=head[u];~i;i=e[i].nxt)
		if(mp[e[i].to]==x) return 1;
	return 0;
}
int main()
{
	memset(head,-1,sizeof(head));
	a=read(),b=read(),c=read();
	x[0]=1;insert(1);
	for(int i=1;i<=2e6;i++) 
	{
		x[i]=(a*x[i-1]+x[i-1]%b)%c;
		if(find(x[i])) {printf("%d",i);return 0;}
		insert(x[i]);
	}
	
	printf("-1");
	return 0;
}

【哈希表模板(链前版本)】【ybtoj】【特殊序列】

原文:https://www.cnblogs.com/conprour/p/15303103.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!