sgu 181 分类: sgu 2015-03-08 11:29 36人阅读 评论(0) 收藏

初看以为要构造矩阵,细看发现构造不了,再细看发现取模的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  
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-03-08 11:29  <Dash>  阅读(114)  评论(0编辑  收藏  举报