Fork me on GitHub

[NOI2012]随机数生成器

[NOI2012]随机数生成器

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

显然矩阵加速递推.

那么我们看一下递推式 $:$

$$f_i = ( a\times f_{i-1} + c ) \% m$$

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

构造矩阵 $:$

初始矩阵为 $:$

$$\left[\begin{array}{llll} f_{n} & c \end{array}\right]$$

目标矩阵为 $:$

$$\left[\begin{array}{llll} f_{n+1} & c \end{array}\right]$$

所以转移矩阵应满足 $:$

$$\left[\begin{array}{llll} f_{n} & c \end{array}\right] \times \left[\begin{array}{llll} ? & ? \ ? & ? \end{array}\right] = \left[\begin{array}{llll} f_{n+1} & c \end{array}\right]$$

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

$$\left[\begin{array}{llll} a & 0 \ 1 & 1 \end{array}\right]$$

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

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

难道使用龟速乘嘛?不!我们用 $__int128!$.然后它就 $AC$ 了.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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\n" , Tans ) ;
system ("pause") ; return 0 ;
}