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. 2. Java中的垃圾收集 - GC参考手册

    标记-清除(Mark and Sweep)是最经典的垃圾收集算法.将理论用于生产实践时, 会有很多需要优化调整的地点, 以适应具体环境.下面通过一个简单的例子, 让我们一步步记录下来, 看看如何才能保 ...

  2. Unity Water Shader

    上图是一个物体浸入水中的效果 原理 我们使用相机渲染的整个场景的深度图减去需要忽略的模型的深度,这里忽略的是图中蓝色部分,就保留了其他的深度值. 用到Main Camera渲染的深度贴图: sampl ...

  3. Mvc Action可以通过jsonp方式调取

    jsonp其实是一种特殊的数据获取格式,所以在Aicton直接调取的时候肯定会出现问题,下面代码是对于jsonp调取做的处理 protected virtual ActionResult Create ...

  4. HDU 3664 (水地推)

    http://acm.hdu.edu.cn/showproblem.php?pid=3664 题意:给出数字n,问n的所有的排列中满足Ai>i 数字恰好为 k的排列的个数. sl : dp dp ...

  5. 2018/3/14 Hadoop学习笔记(一)

    首先,什么是Hadoop?为什么它是现在大数据处理最热门的框架呢?(正确来说,现在Hadoop是一个生态圈) Hadoop是apache下一套开源的服务框架,它主要的作用就是利用服务器集群,来对海量数 ...

  6. msp430入门学习11

    msp430的定时器--看门狗 msp430入门学习

  7. Layui栅格系统与后台框架布局

    一.栅格布局规则: 1. 采用 layui-row 来定义行,如:<div class="layui-row"></div> 2. 采用类似 layui-c ...

  8. Remove Element(第一种方法参考别人)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  9. CSS+Jquery实现QQ分组列表

    实现效果图如下: 说明: 1.css隐藏分组下的好友内容: 2.Jquery实现点击分组项事件,实现好友内容的显示和隐藏: 3.样式1,可展开多个分组:样式2,只能有一个分组展开: 源码: <! ...

  10. Java File类 mkdir 不能创建多层目录,如果是多层,可以调mkdirs

    public static void createDir(String destDirName) { File dir = new File(destDirName); if (!dir.exists ...