Consider an n-by-n matrix A. We define Ak = A ∗ A ∗ . . . ∗ A (k times). Here, ∗ denotes the usual matrix multiplication. You are to write a program that computes the matrix A + A2 + A3 + . . . + Ak . Example Suppose A =   0 2 0 0 0 2 0 0 0  . Then A2 =   0 2 0 0 0 2 0 0 0     0 2 0 0 0 2 0 0 0   =   0 0 4 0 0 0 0 0 0  , thus: A + A2 =   0 2 0 0 0 2 0 0 0   +   0 0 4 0 0 2 0 0 0   =   0 2 4 0 0 2 0 0 0   Such computation has various applications. For instance, the above example actually counts all the paths in the following graph: Input Input consists of no more than 20 test cases. The first line for each case contains two positive integers n (≤ 40) and k (≤ 1000000). This is followed by n lines, each containing n non-negative integers, giving the matrix A. Input is terminated by a case where n = 0. This case need NOT be processed. Output For each case, your program should compute the matrix A + A2 + A3 + . . . + Ak . Since the values may be very large, you only need to print their last digit. Print a blank line after each case. Sample Input 3 2 0 2 0 0 0 2 0 0 0 0 0 Sample Output 0 2 4 0 0 2 0 0 0

矩阵快速幂+分治。。。

很巧妙啊 先开始还在想怎么错位相减。。。

具体细节不讲了 代码里都有 这道题给我们的启示是碰见这种连续幂相加的东西要想分治。。。

先开始t了半天,结果写成暴力了。。。

#include<bits/stdc++.h>
using namespace std;
const int N = ;
struct mat {
int a[N][N];
} A;
int n, k;
mat operator * (mat A, mat B)
{
mat ret; memset(ret.a, , sizeof(ret.a));
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
for(int k = ; k <= n; ++k) ret.a[i][j] = (ret.a[i][j] + A.a[i][k] % * B.a[k][j] % ) % ;
return ret;
}
mat power(mat x, int t)
{
mat ret; memset(ret.a, , sizeof(ret.a));
for(int i = ; i <= n; ++i) ret.a[i][i] = ;
for(; t; t >>= , x = x * x) if(t & ) ret = ret * x;
return ret;
}
mat operator + (mat A, mat B)
{
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j) A.a[i][j] = (A.a[i][j] + B.a[i][j]) % ;
return A;
}
mat solve(int t)
{
if(t == ) return A;
mat x = solve(t / ), ret = x, B = power(A, t / );
if(t & ) return x + (x + B * A) * B;
else return x + x * B;
}
int main()
{
while(scanf("%d%d", &n, &k))
{
if(n == ) break;
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j) scanf("%d", &A.a[i][j]), A.a[i][j] %= ;
mat x = solve(k);
for(int i = ; i <= n; ++i)
{
for(int j = ; j < n; ++j) printf("%d ", x.a[i][j]);
printf("%d\n", x.a[i][n]);
}
puts("");
}
return ;
}

uva11149的更多相关文章

  1. UVA11149 Power of Matrix —— 矩阵倍增、矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-11149 题意: 给出矩阵A,求出A^1 + A^2 …… + A^k . 题解: 1.可知:A^1 + A^2 …… + A ...

  2. UVA11149 矩阵快速幂

    首先我们来想一下计算A+A^2+A^3...+A^k. 如果A=2,k=6.那你怎么算 2+22+23+24+25+26 = ?= (2+22+23)*(1+23) 如果A=2,k=7.那你怎么算 2 ...

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

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

  4. UVA-11149 Power of Matrix(矩阵二分幂)

    题目大意:给一个n阶方阵,求A1+A2+A3+......Ak. 题目分析:令F(k)=A1+A2+A3+......Ak.当k为偶数时,F(k)=F(k/2)*(E+Ak/2),k为奇数时,F(k) ...

  5. uva11149矩阵快速幂

    求A+A^1+...+A^n 转换一下变成|A  E|,的n+1次方就是|A^(n+1)  A^n+...+A+E| |0  E|                       |    0       ...

  6. UVa 11149 矩阵的幂(矩阵倍增法模板题)

    https://vjudge.net/problem/UVA-11149 题意: 输入一个n×n矩阵A,计算A+A^2+A^3+...A^k的值. 思路: 矩阵倍增法. 处理方法如下,一直化简下去直到 ...

  7. HDU2243 考研路茫茫——单词情结 ——AC自动机、矩阵优化

    题目链接:https://vjudge.net/problem/HDU-2243 考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  8. 矩阵乘法优化DP复习

    前言 最近做毒瘤做多了--联赛难度的东西也该复习复习了. Warning:本文较长,难度分界线在"中场休息"部分,如果只想看普及难度的可以从第五部分直接到注意事项qwq 文中用(比 ...

随机推荐

  1. ruby rspec安装

    在rubymine里新建Rails application

  2. CSS3 @font-face的url要添加?#iefix的原因

    转至:https://github.com/CSSLint/csslint/wiki/Bulletproof-font-face When using @font-face to declare mu ...

  3. PHP:压缩 Zip

    文章来源:http://www.cnblogs.com/hello-tl/p/7661222.html <?php # 文件字符集 header("Content-type: text ...

  4. Spring Boot 2 (三):Spring Boot 开源软件都有哪些?

    016年 Spring Boot 还没有被广泛使用,在网上查找相关开源软件的时候没有发现几个,到了现在经过2年的发展,很多互联网公司已经将 Spring Boot 搬上了生产,而使用 Spring B ...

  5. win10 默认锁屏路径

    C:\Users\YourUsername\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\ ...

  6. 【ZJOI2017 Round1练习&UVA1057】D6T1 Routing(DP,SPFA)

    题意:给你一个有向图, 并指定起点和终点. 问要从起点走向终点, 再从终点走向起点, 最少需要走过多少不同的节点. 对于 100%的数据, 有 N<=100, M<=min(1000,N* ...

  7. 【进击后端】ubuntu 快速安装node mongodb express

    安装软件:node,mongo,express 1.apt install node 2.node -v 3.apt install mongodb 4.mongo -version 5.apt in ...

  8. [bzoj2733][HNOI2012]永无乡_权值线段树_线段树合并

    永无乡 bzoj-2733 HNOI-2012 题目大意:题目链接. 注释:略. 想法: 它的查询操作非常友善,就是一个联通块内的$k$小值. 故此我们可以考虑每个联通块建一棵权值线段树. 这样的话每 ...

  9. Servlet实现点击计数器

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/hits-counter.html: 一.Web页面的点击计数器 很多时候,可能有兴趣知道网站的某 ...

  10. 【转】深入理解javascript作用域——词法作用域和动态作用域

    前面的话 大多数时候,我们对作用域产生混乱的主要原因是分不清楚应该按照函数位置的嵌套顺序,还是按照函数的调用顺序进行变量查找.再加上this机制的干扰,使得变量查找极易出错.这实际上是由两种作用域工作 ...