Fast Power, matrix multiplication, and rapid power of a Matrix

Source: Internet
Author: User

Fast power usage binary

Complexity log-level

#include <cstdio>#include <iostream>#include <string>#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef unsigned long long ull;int q_power(int a,int b,int c) {    int r=1;    a%=c;    while (b) {        if (b&1) {            r=(r*a)%c;        }        a=(a*a)%c;        b>>=1;    }    return r;}int a,b,c;int main () {    cin>>a>>b>>c;    cout<<q_power(a,b,c);    return 0;}

Attach the matrix's quick power and

Matrix fast idempotent Fibonacci series:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;const int mod = 10000;const int maxn = 35;int N;struct Matrix {    int mat[maxn][maxn];    int x, y;    Matrix() {        memset(mat, 0, sizeof(mat));        for (int i = 1; i <= maxn - 5; i++) mat[i][i] = 1;    }};inline void mat_mul(Matrix a, Matrix b, Matrix &c) {    memset(c.mat, 0, sizeof(c.mat));    c.x = a.x; c.y = b.y;    for (int i = 1; i <= c.x; i++) {        for (int j = 1; j <= c.y; j++) {            for (int k = 1; k <= a.y; k++) {                c.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % mod;                c.mat[i][j] %= mod;            }        }    }    return ;}inline void mat_pow(Matrix &a, int z) {    Matrix ans, base = a;    ans.x = a.x; ans.y = a.y;    while (z) {        if (z & 1 == 1) mat_mul(ans, base, ans);        mat_mul(base, base, base);        z >>= 1;    }    a = ans;}int main() {    while (cin >> N) {        switch (N) {            case -1: return 0;            case 0: cout << "0" << endl; continue;            case 1: cout << "1" << endl; continue;            case 2: cout << "1" << endl; continue;        }        Matrix A, B;        A.x = 2; A.y = 2;        A.mat[1][1] = 1; A.mat[1][2] = 1;        A.mat[2][1] = 1; A.mat[2][2] = 0;        B.x = 2; B.y = 1;        B.mat[1][1] = 1; B.mat[2][1] = 1;        mat_pow(A, N - 1);        mat_mul(A, B, B);        cout << B.mat[1][1] << endl;    }    return 0;}

By the way

Matrix Multiplication:

/* Assume that A is a matrix of M * P, and B is a matrix of p * n. c = AB (C is the product of matrix A and B) then C is the matrix of M * n */For (INT I = 1; I <= m; ++ I) // the row of a {for (Int J = 1; j <= N; ++ J) // column {for (int K = 1; k <= P; ++ K) of B) // use the formula C {C [I] [J] + = A [I] [k] * B [k] [J] ;}}
# Include <iostream> # include <cmath> # include <cstring> using namespace STD; typedef long ll; const ll mod = 1000000007; /* matrix fast power evaluate Fibonacci series input N output f [N] */struct mat {ll mat [2] [2] ;}; mat operator * (MAT, mat B) // matrix multiplication {mat C; For (INT I = 0; I <2; ++ I) {for (Int J = 0; j <2; ++ J) {C. mat [I] [J] = 0; For (int K = 0; k <2; ++ K) {C. mat [I] [J] = (. mat [I] [k] * B. mat [k] [J]) % mod + C. mat [I] [J]) % mod ;}} return C;} mat operator ^ (MAT a, ll K) // matrix power {mat C; for (INT I = 0; I <2; ++ I) {for (Int J = 0; j <2; ++ J) {C. mat [I] [J] = (I = J); // The unit matrix is initialized} // It is said that the value of any matrix multiplied by the unit matrix will not change to (; K; k> = 1) {If (K & 1) C = C * A; A = A * A;} return C ;}int main () {ll N; while (CIN> N) {mat A;. mat [0] [0] = 1,. mat [0] [1] = 1,. mat [1] [0] = 1,. mat [1] [1] = 0; MAT fn = a ^ N; cout <fn. mat [0] [1] <Endl;} return 0 ;}

 

Fast Power, matrix multiplication, and rapid power of a Matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.