初看以为要构造矩阵,细看发现构造不了,再细看发现取模的m很小
于是就哈希找函数周期了。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#define INF (1<<30)-1
const int MAXM = 1005;
int hash[MAXM] = {0};
long long x[MAXM<<1] = {0};
int len = 0 ,st = 0;
int A ,a ,b ,c ,M ,k ;
int main()
{
int ans = 0;
#ifndef ONLINE_JUDGE
freopen("sgu181.in","r",stdin);
freopen("sgu181.out","w",stdout);
#endif
scanf("%d%d%d%d%d%d",&A,&a,&b,&c,&M,&k);
memset(hash,-1,sizeof(hash));
x[0] = A; if(A < M)hash[A] = 0;
for(int i = 1; i <= k ; i++)
{
x[i] = (a*x[i-1]*x[i-1] + b*x[i-1] +c) % M;
if(hash[x[i]] == -1){hash[x[i]] = i;}
else
{
len = i - hash[x[i]] ;
st = hash[x[i]];
break;
}
}
if(len == 0)ans = x[k];
else ans = x[st + (k - st)%len];
printf("%d",ans);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
}版权声明:本文为博主原创文章,未经博主允许不得转载。
本文介绍了一种利用哈希表寻找数列周期的方法,针对特定取模m下的数列生成问题,通过记录数列中元素首次出现的位置,确定周期起点和长度,从而高效求解任意位置的数列值。
195

被折叠的 条评论
为什么被折叠?



