[2016-02-04][矩阵快速幂]


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

#define MOD 10000

const int mat_size = 2;

struct matrix{

        long long a[mat_size][mat_size];

};

matrix matrixE;//单位矩阵

void inimatrixE(){

        for(int i = 0;i < mat_size;++i)

                for(int j = 0;j < mat_size;++j)

                        matrixE.a[i][j] = i == j ? 1 : 0;

}

void mat_mult(matrix & a,matrix & b,matrix & c,int m,int n,int s,long long mod){

        //a == m*n      b == n*s        c == m*s

        memset(c.a,0,sizeof(c.a));

        for(int i = 0 ;i < m ; ++i)

                for(int k = 0;k < n ; ++k)

                        for(int j = 0 ; j < s ; ++j){

                                c.a[i][j] = (c.a[i][j] + a.a[i][k]*b.a[k][j]) % mod;

//如果结果不会超过longlong范围,那么取模运算可以放在第二个for内(第3个for 外面)

                        }

}

void quick_matrix_pow(matrix & a,int p,matrix & res){

        //inimatrixE();

        memset(res.a,0,sizeof(res.a));

        matrix tmp = a,tmpres;

        res = matrixE;

        while(p >= 1){

                if(p & 1){

                        mat_mult(tmp,res,tmpres,mat_size,mat_size,mat_size,MOD);

                        res = tmpres;

                }

                p >>= 1;

                mat_mult(tmp,tmp,tmpres,mat_size,mat_size,mat_size,MOD);

                tmp = tmpres;

        }

}

来自为知笔记(Wiz)

时间: 2024-10-15 01:06:37

[2016-02-04][矩阵快速幂]的相关文章

2016&quot;百度之星&quot; - 初赛(Astar Round2A) A.All X 矩阵快速幂

All X Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description F(x, m)F(x,m) 代表一个全是由数字xx组成的mm位数字.请计算,以下式子是否成立: F(x,m)\ mod\ k\ \equiv \ cF(x,m) mod k ≡ c Input 第一行一个整数TT,表示TT组数据. 每组测试数据占一行,包含四个数字x,m,k,cx,m,

2016 pku campusH/OpenJ_POJ - C16H(推公式+矩阵快速幂)

传送门:http://poj.openjudge.cn/practice/C16H?lang=en_US 题面:描述 Wenwen has a magical ball. When put on an infinite plane, it will keep duplicating itself forever. Initially, Wenwen puts the ball on the location (x0, y0) of the plane. Then the ball starts

初步 - 矩阵快速幂

2017-09-05 22:26:02 writer:pprp 参考:http://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html 这位大佬写的很简单易懂 关键代码如下: while(N) { if(N&1) res=res*A; n>>=1; A=A*A; } 具体代码: /* @theme:矩阵快速幂 @writer:pprp @begin:21:45 @end:22:25 @declare: @date:2017/9

POJ3070 Fibonacci(矩阵快速幂)

用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Time :2016/1/19 20:11:43 ************************************************ */ #include <iostream> #include <algorithm> #include <cstring> #in

BNUOJ 34985 Elegant String 2014北京邀请赛E题 动态规划 矩阵快速幂

Elegant String Time Limit: 1000msMemory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main We define a kind of strings as elegant string: among all the substrings of an elegant string, none of them is a permutation of "0, 1,-, k

hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law. Through unremitting e

hdu 3306 Another kind of Fibonacci(矩阵快速幂)

Another kind of Fibonacci                                                        Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1691    Accepted Submission(s): 660 Problem Description As we al

[POJ 3150] Cellular Automaton (矩阵快速幂 + 矩阵乘法优化)

Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 1227 Case Time Limit: 2000MS Description A cellular automaton is a collection of cells on a grid of specified shape that evolves through a number of dis

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const