• BZOJ2875 [Noi2012]随机数生成器


    2875: [Noi2012]随机数生成器

    Time Limit: 10 Sec  Memory Limit: 512 MB
    Submit: 1388  Solved: 772
    [Submit][Status][Discuss]

    Description

        栋栋最近迷上了随机算法,而随机数是生成随机算法的基础。栋栋准备使用线性同余法(Linear Congruential Method)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a,c,X[0],按照下面的公式生成出一系列随机数{Xn}:

                               X[n+1]=(aX[n]+c) mod m

        其中mod m表示前面的数除以m的余数。从这个式子可以看出,这个序列的下一个数总是由上一个数生成的。

        用这种方法生成的序列具有随机序列的性质,因此这种方法被广泛地使用,包括常用的C++和Pascal的产生随机数的库函数使用的也是这种方法。

        栋栋知道这样产生的序列具有良好的随机性,不过心急的他仍然想尽快知道X[n]是多少。由于栋栋需要的随机数是0,1,...,g-1之间的,他 需要将X[n]除以g取余得到他想要的数,即X[n] mod g,你只需要告诉栋栋他想要的数X[n] mod g是多少就可以了。

     

    Input

    包含6个用空格分割的m,a,c,X0,n和g,其中a,c,X0是非负整数,m,n,g是正整数。

    Output

    输出一个数,即Xn mod g

    Sample Input


    11 8 7 1 5 3


    Sample Output

    2

    HINT

     

    Source

     

    【思路】

      矩阵乘法+处理long long相乘。

      初始矩阵为:

    | xn , 1|

      转移矩阵为:

    | a, 0 |

    | c, 1 |

     Longlong相乘的处理大体思路是化除为乘。

    【代码】

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 using namespace std;
     5 
     6 typedef long long LL;
     7 LL n,a,c,g,x0,m;
     8 
     9 LL multi( LL y , LL cnt ) {
    10     if ( ! cnt ) return 0 ;
    11     if ( cnt == 1 ) return y % m ;
    12     LL rec = multi( y , cnt / 2 ) ;
    13     rec = ( rec + rec ) % m ;
    14     if ( cnt % 2 ) rec = ( rec + y ) % m ;
    15     return rec ;
    16 }
    17 struct Matrix{
    18     int r,c;
    19     LL N[5][5];
    20     void init(int r,int c) {
    21         this->r=r, this->c=c;
    22         memset(N,0,sizeof(N));
    23     }
    24     Matrix operator*(Matrix& B)const{
    25         Matrix C;
    26         C.init(r,B.c);
    27         for(int i=0;i<C.r;i++)
    28            for(int j=0;j<C.c;j++)
    29            {
    30               for(int k=0;k<c;k++) C.N[i][j] += multi(N[i][k],B.N[k][j]);
    31               C.N[i][j]%=m;
    32           }
    33         return C;
    34     }
    35     Matrix pow(LL p) {
    36         Matrix tmp=*this;
    37         Matrix ans;
    38         ans.init(r,r);
    39         for(int i=0;i<r;i++) ans.N[i][i]=1;
    40         while(p) {
    41             if(p&1) ans=ans*tmp;
    42             tmp=tmp*tmp;
    43             p>>=1;
    44         }
    45         return ans;
    46     }
    47 };
    48 Matrix ans,T;
    49 
    50 int main() {
    51     ios::sync_with_stdio(false);
    52     cin>>m>>a>>c>>x0>>n>>g;
    53     ans.init(1,2);
    54     ans.N[0][0]=x0,ans.N[0][1]=1;
    55     T.init(2,2);
    56     T.N[0][0]=a,T.N[0][1]=0,T.N[1][0]=c,T.N[1][1]=1;
    57     T=T.pow(n);
    58     ans=ans*T;
    59     cout<<ans.N[0][0]%g;
    60     return 0;
    61 }
  • 相关阅读:
    Buffer Lock Mode and Compatibilities
    11g默认审计选项
    11.2.0.2 asmcmd lsdg show incorrect diskgroup number
    关于V$OPEN_CURSOR
    了解你所不知道的SMON功能(七):清理IND$字典基表
    PL/SQL DEPENDENCIES
    温习 Linux 命令
    温习 SQL 03
    PL/SQL 基础 ( 下 )
    HTML501 ( The.Essential.Guide )
  • 原文地址:https://www.cnblogs.com/lidaxin/p/4923405.html
Copyright © 2020-2023  润新知