Power of Matrix 等比数列求和 矩阵版!

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<string>
using namespace std;
typedef long long  LL;
typedef unsigned long long ULL;
#define MAXN 49
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-9;
//矩阵快速幂
int n, k;
struct Mat
{
    int a[MAXN][MAXN];
    Mat()
    {
        memset(a, 0, sizeof(a));
    }
    Mat operator *(const Mat& rhs)
    {
        Mat ret;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (a[i][j])
                {
                    for (int t = 0; t < n; t++)
                    {
                        ret.a[i][t] = (ret.a[i][t] + a[i][j] * rhs.a[j][t])%10;
                    }
                }
            }
        }
        return ret;
    }
    Mat operator +(const Mat& rhs)
    {
        Mat ret;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                ret.a[i][j] += (a[i][j] + rhs.a[i][j])%10;
            }
        }
        return ret;
    }
};
Mat e;
Mat fpow(const Mat& m, int b)
{
    Mat ans, tmp = m;
    for (int i = 0; i < n; i++)
        ans.a[i][i] = 1;
    while (b != 0)
    {
        if (b & 1)
            ans = tmp*ans;
        tmp = tmp * tmp;
        b >>= 1;
    }
    return ans;
}
Mat sum(const Mat& m, int k)
{
    if (k == 1) return m;
    else if (k % 2 == 0)
    {
        return (e + fpow(m, k / 2)) * sum(m, k / 2);
    }
    else if (k % 2 == 1)
    {
        Mat tmp = fpow(m, k / 2 + 1);
        return    (e + tmp)*sum(m, k / 2) + tmp;
    }
}

int main()
{
    while (scanf("%d%d", &n,&k), n)
    {
        for (int i = 0; i < n; i++)
            e.a[i][i] = 1;
        Mat M;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                scanf("%d", &M.a[i][j]);
                M.a[i][j] %= 10;
            }
        }
        M = sum(M, k);
        for (int i = 0; i < n; i++)
        {
            printf("%d", M.a[i][0]);
            for (int j = 1; j < n; j++)
            {
                printf("% d", M.a[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
}
时间: 2024-10-27 04:29:20

Power of Matrix 等比数列求和 矩阵版!的相关文章

Power of Matrix(uva11149+矩阵快速幂)

Power of Matrix Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11149 Appoint description:  System Crawler  (2015-03-15) Description Problem B : Power of Matrix Time limit: 10 seconds Consider an n-by

UVA 11149 - Power of Matrix(矩阵倍增)

UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞,∑kiAi=(1+Ak/2)∑k/2iAi,不断二分即可 代码: #include <cstdio> #include <cstring> const int N = 45; int n, k; struct mat { int v[N][N]; mat() {memset(v, 0, sizeof(v));} mat operator * (mat

[NOI2011] 兔农 矩阵乘法,矩阵的等比数列求和

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; ll n,p; ll k; #define N 4000000 ll a[N],fr[N]; struct sq{ ll a[3][3]; sq(){memset(a,0,sizeof(a));} sq operator*(sq

Uva 11149 - Power of Matrix ( 矩阵快速幂 )

Uva 11149 -Power of Matrix ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 10 #define MAX_SIZE 40 struct Mat { int r, c; int mat[MAX_SIZE][MAX_SIZE]; Mat( int _r = 0 , int _c = 0 ) { CLR

SPOJ AMR10E Stocks Prediction --二分求和+矩阵快速幂

题意:给一个递推式S(n) = a1*S(n-1)+...+aR*S(n-R),要求S(k)+S(2k)+...+S(nk)的值. 分析:看到n的大小和递推式,容易想到矩阵快速幂.但是如何转化呢? 首先看到 我们用A表示上面的递推式中的R*R的那个矩阵,那么对于前面那个向量,每次乘上A^k之后都会变成(S(n + k)...)那么对于初始的向量( S(R) S(R - 1) ... S(1) ) 如果这个向量当中包括 S(k) 我们可以直接对于每次要算的 S( i * k) 求和也就是说这个向量

HDU 4965 Fast Matrix Calculation 【矩阵】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4965 题目大意:给你一个N*K的矩阵A以及一个K*N的矩阵B (4 <= N <= 1000)以及 (2 <=K <= 6),然后接下来四步: 算一个新的矩阵C=A*B 算M=C^ (N*N) 对于M中的每个元素%6 将M中每个元素加起来,算出和. 也就是求出A*B * A*B * A*B * A*B * A*B *--* A*B   但是A*B形成的矩阵是N*N,而N大小有可能是10

leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 真的不容易..看博客园有人面试碰到过这个问题,长篇

HDU 4965 Fast Matrix Calculation(矩阵快速幂)

HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次,可以变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个只有6x6,就可以用矩阵快速幂搞了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int M = 10; int n, m; int A[N][M], B[M][N], C[M][M], CC[N]

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr