Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Change MRows,MCols
- 1. Call init(row,col)
- 2. Build Initial Matrix
- 3. Expo
- */
- const int MRows = 2;
- const int MCols = 2;
- typedef long long m_t;
- long long MOD;
- struct Matrix {
- int r, c;
- m_t m[MRows][MCols];
- void init(int R, int C) {
- memset(m, 0, sizeof m);
- r=R; c=C;
- }
- void iden() {
- for(int i = 0 ; i < r; i ++) m[i][i] = 1;
- }
- void print() {
- puts("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
- for (int i = 0; i < r; ++i) {
- for (int j = 0; j < c; ++j) printf("%4d ", m[i][j]);
- printf("\n");
- }
- puts("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
- }
- void mul(const Matrix &y, Matrix &z) const {
- // value Stored in Z
- z.r = r, z.c = y.c; m_t v;
- for (int i = 0; i < z.r; ++i)
- for (int j = 0; j < z.c; ++j) {
- v = 0;
- for (int k = 0; k < c; ++k){
- v += m[i][k] * y.m[k][j];
- v %= MOD;
- }
- z.m[i][j] = v;
- }
- }
- void add(const Matrix &B , Matrix &c) {
- c.r = c.c = r;
- for(int i = 0 ; i < c.r ; i ++ ){
- for(int j = 0 ; j <c.c ; j ++ ) {
- c.m[i][j] = m[i][j] + B.m[i][j];
- }
- }
- }
- void exp(long long e, Matrix &z) {
- //value stored in Z
- z.init(r, c); z.iden();
- Matrix x, b = *this;
- while (true) {
- if (e & 1) { z.mul(b, x); z = x; }
- e >>= 1;
- if (e == 0) break;
- b.mul(b, x);
- b = x;
- }
- }
- };
Add Comment
Please, Sign In to add comment