题目链接: 传送门

Power of Matrix

Time Limit: 3000MS     

Description


给一个n阶方阵,求A1+A2+A3+......Ak。

思路

A1+A2+...+An = (A1+A2+...+An/2)+(A1+A2+...+An/2) * An/2 = (1 + An/2 ) * (A1+A2+...+An/2)那么对于 (A1+A2+...+An/2)也能用同样的方法去求,不断对半下去计算,最后总体复杂度为log(n)^2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 50;
const int mod = 10;

struct Matrix
{
    int mat[50][50];
    int r,c;
    Matrix(int r1 = 0,int c1 = 0):r(r1),c(c1)
    {
        memset(mat,0,sizeof(mat));
    }
    void E()
    {
        memset(mat,0,sizeof(mat));
        for (int i = 0;i < r;i++)
        {
            for (int j = 0;j < c;j++)
            {
                mat[i][j] = (i == j);
            }
        }
    }
    Matrix operator+(const Matrix & m)
    {
        Matrix res(r,c);
        for (int i = 0; i < r; i++)
        {
            for (int j = 0; j < c; j++)
            {
                res.mat[i][j] = (mat[i][j] + m.mat[i][j])%mod;
            }
        }
        return res;
    }

    Matrix operator * (const Matrix & m)
    {
        Matrix res(r,m.c);
        for (int i = 0; i < r; i++)
        {
            for (int j = 0; j < m.c; j++)
            {
                for (int k = 0; k < c; k++)
                {
                    res.mat[i][j] = (res.mat[i][j] + mat[i][k] * m.mat[k][j])%mod;
                }
            }
        }
        return res;
    }
    void show()
    {
        for (int i = 0;i < r;i++)
        {
            bool first = true;
            for (int j = 0;j < c;j++)
            {
                first?printf("%d",mat[i][j]):printf(" %d",mat[i][j]);
                first = false;
            }
            printf("\n");
        }
    }
};

Matrix pow(Matrix x,int n)
{
    Matrix res(x.r,x.c);
    res.E();
    while (n > 0)
    {
        if (n&1)
        {
            res = res * x;
        }
        x = x * x;
        n >>= 1;
    }
    return res;
}

Matrix sum(Matrix mat,int k)
{
    if (k == 1)
    {
        return mat;
    }
    Matrix E(mat.r,mat.c);
    E.E();
    if (k&1)
    {
        return (E + pow(mat,k/2))*sum(mat,k/2) + pow(mat,k);
    }
    else
    {
        return (E + pow(mat,k/2))*sum(mat,k/2);
    }
}

int main()
{
    int n,k;
    while (~scanf("%d%d",&n,&k) && n && k)
    {
        Matrix Mat(n,n);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                scanf("%d",&Mat.mat[i][j]);
                Mat.mat[i][j] %= mod;
            }
        }
        if (k == 0)
        {
            Mat.show();
            continue;
        }
        Mat = sum(Mat,k);
        Mat.show();
        printf("\n");
    }
    return 0;
}

UVa 11149 Power of Matrix(倍增法、矩阵快速幂)的更多相关文章

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

    Power of Matrix Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit St ...

  2. UVA 11149 - Power of Matrix(矩阵乘法)

    UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞.∑kiAi=(1+Ak/2)∑k/2iAi,不断二分就可以 代码: ...

  3. UVA 11149 Power of Matrix

    矩阵快速幂. 读入A矩阵之后,马上对A矩阵每一个元素%10,否则会WA..... #include<cstdio> #include<cstring> #include< ...

  4. POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】

    典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...

  5. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

  6. HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...

  7. UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)

    题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...

  8. UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)

    题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...

  9. UVA 11149 Power of Matrix 快速幂

    题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...

随机推荐

  1. .net RPC框架选型

    近期开始研究分布式架构,会涉及到一个最核心的组件:RPC(Remote Procedure Call Protocol).这个东西的稳定性与性能,直接决定了分布式架构系统的好坏.RPC技术,我们的产品 ...

  2. Redis简介-安装-入门

    前言 我们team马上要用Redis了. 所以先学习一下这东西. Redis大名很早以前就听过了,以前在的公司都没有用到. 现在有机会终于接触到了,果断学习起来. 什么是redis Redis是完全开 ...

  3. HoloLens开发手记 - Vuforia开发概述 Vuforia development overview

    关于Vuforia,开发AR应用的人基本都会熟悉.之前我也写过一篇关于Vuforia开发的博客:Vuforia AR SDK入门 今天这篇博客则主要是谈谈HoloLens使用Vuforia开发混合现实 ...

  4. Reflection和Expression Tree解析泛型集合快速定制特殊格式的Json

    很多项目都会用到Json,而且大部分的Json都是格式固定,功能强大,转换简单等,标准的key,value集合字符串:直接JsonConvert.SerializeObject(List<T&g ...

  5. 清除webBrowser 缓存和Cookie的解决方案

    通过测试webBrowser与IE缓存和Cookie都存放在Local Settings\Temporary Internet Files,我们可以直接调用IE API进行清除 解决方案1: publ ...

  6. MinHash算法

    MinHash是用于快速检测两个集合的相似性的方法.改方法由Andrei Broder(1997)发明,并最初用于搜索引擎AltaVista中来检测重复的网页的算法.它同样可以用于推荐系统和大规模文档 ...

  7. Hibernate之Annotation(注解的方式,非映射)

    在hibernate 3.0之后,可以建立一个符合JPA标准的Annotation,以hibernate3.3.2GA为例 Annotation 以 hibernate Annotation 3.3. ...

  8. 50ms延时程序

    12MHz晶振 一个机器周期2us, DEL:  MOV  R7,#200D DEL1: MOV R6,#125 DEL2: DJNZ R6,DEL2 ;125*2=250us DJNZ  R7,DE ...

  9. Java设计模式(七) 模板模式-使用钩子

    1,模板类 package com.pattern.template; public abstract class CaffeineBeverageWithHook { void prepareRec ...

  10. 【BZOJ 1043】【HNOI 2008】下落的圆盘 判断圆相交+线段覆盖

    计算几何真的好暴力啊. #include<cmath> #include<cstdio> #include<cstring> #include<algorit ...