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 ;
}

最新文章

  1. Ngui中Sprite,SlicedSprite,Tiled Sprite,FilledSprite的区别
  2. TI BLE CC2541的通讯协议.
  3. 通过VMName获取VM IP
  4. linux-多线程
  5. 【持久化框架】Mybatis与Hibernate的详细对比
  6. 2014年CCNU-ACM暑期集训总结
  7. 【Cocos2d-X开发学习笔记】第05期:渲染框架之布景层类(CCLayer)的使用
  8. 移动端登录页面input获取焦点后页面布局及输入框上移的问题
  9. Jquery.ajax dataType参数
  10. vue-router 之 keep-alive
  11. bootstrap概述
  12. BZOJ4738 : 汽水
  13. springboot2.X访问静态文件配置
  14. java httpclient post xml demo
  15. P2700 逐个击破 最小生成树
  16. 【BZOJ3585】mex
  17. [Lydsy1805月赛]口算训练 BZOJ5358
  18. 修改 input[type=&quot;radio&quot;] 和 input[type=&quot;checkbox&quot;] 的默认样式
  19. 一个关于react-native的demo,详细请转GitHub
  20. python面向对象(七)属性方法的添加

热门文章

  1. Mac os安装MySQL数据库,系统提示mysql: command not found该怎么办
  2. First C program
  3. Mybatis_HelloWorld
  4. 【(待重做)树状数组+dp+离散化】Counting Sequences
  5. 解决json_encode中文乱码
  6. 树形DP 树的最小支配集,最小点覆盖与最大独立集
  7. POJ 3169_Layout
  8. CLR GC
  9. NOIP 2010 乌龟棋
  10. Ubuntu 16.04下SecureCRT无法输入中文的解决思路