[NOI2012]随机数生成器

[NOI2012]随机数生成器

给定一个数列的首项及其递推式,求第 (n) 项模 (g) 的结果.

显然矩阵加速递推.

那么我们看一下递推式 (:)

[f_i = ( a imes f_{i-1} + c ) \% m ]

带有模是无所谓的,直接取模就好了.

构造矩阵 (:)

初始矩阵为 (:)

[left[egin{array}{llll} f_{n} & c end{array} ight] ]

目标矩阵为 (:)

[left[egin{array}{llll} f_{n+1} & c end{array} ight] ]

所以转移矩阵应满足 (:)

[left[egin{array}{llll} f_{n} & c end{array} ight] imes left[egin{array}{llll} ? & ? \ ? & ? end{array} ight] = left[egin{array}{llll} f_{n+1} & c end{array} ight] ]

根据递推式,可以得到转移矩阵是 (:)

[left[egin{array}{llll} a & 0 \ 1 & 1 end{array} ight] ]

然后我就直接一发矩阵快速幂莽了上去,然鹅,它 (WA) 了,只有 (50pts).

经过思考后发现,它中间溢出 (long: long) 了.怎么办?

难道使用龟速乘嘛?不!我们用 (\_\_int128!).然后它就 (AC) 了.

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define one first
#define two second
#define rint read<int>
#define int __int128
#define pb push_back
#define db double
#define ull unsigned long long
#define lowbit(x) ( x & ( - x ) )

using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
       while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
       return f * x ;
    }

int m , a , c , fir , n , g ;

struct Matrix {
    int M[3][3] , line , row ;
    inline void clear () { MEM ( M , 0 ) ; line = row = 0 ; return ; }
    friend Matrix operator * (Matrix a , Matrix b) {
        Matrix res ; res.clear () ; res.line = a.line ; res.row = b.row ;
        rep ( k , 1 , a.row ) rep ( i , 1 , a.line ) rep ( j , 1 , b.row )
            res.M[i][j] = ( res.M[i][j] + a.M[i][k] * b.M[k][j] % m ) % m ;
        return res ;
    }
    friend Matrix operator ^ (Matrix a , int p) {
        Matrix res = a ;
        for ( -- p ; p ; a = a * a , p >>= 1ll)
            if ( p & 1 ) res = res * a ;
        return res ;
    }
} e , ans ;

signed main (int argc , char * argv[]) {
    m = rint () ; a = rint () ; c = rint () ;
    fir = rint () ; n = rint () ; g = rint () ;
    ans.clear () ; e.clear () ; e.line = e.row = 2 ;
    e.M[1][1] = a ; e.M[2][1] = 1 ; e.M[2][2] = 1 ;
    ans.line = 1 ; ans.row = 2 ;
    ans.M[1][1] = fir ; ans.M[1][2] = c ;
    ans = ans * ( e ^ n ) ;
    long long Tans = ans.M[1][1] % g ;
    printf ("%lld
" , Tans ) ;
    system ("pause") ; return 0 ;
}
原文地址:https://www.cnblogs.com/Equinox-Flower/p/11756669.html