博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva11149
阅读量:4327 次
发布时间:2019-06-06

本文共 2607 字,大约阅读时间需要 8 分钟。

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
using namespace std;const int N = 41;struct mat { int a[N][N];} A;int n, k;mat operator * (mat A, mat B){ mat ret; memset(ret.a, 0, sizeof(ret.a)); for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) for(int k = 1; k <= n; ++k) ret.a[i][j] = (ret.a[i][j] + A.a[i][k] % 10 * B.a[k][j] % 10) % 10; return ret; }mat power(mat x, int t){ mat ret; memset(ret.a, 0, sizeof(ret.a)); for(int i = 1; i <= n; ++i) ret.a[i][i] = 1; for(; t; t >>= 1, x = x * x) if(t & 1) ret = ret * x; return ret;}mat operator + (mat A, mat B){ for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) A.a[i][j] = (A.a[i][j] + B.a[i][j]) % 10; return A; }mat solve(int t){ if(t == 1) return A; mat x = solve(t / 2), ret = x, B = power(A, t / 2); if(t & 1) return x + (x + B * A) * B; else return x + x * B; }int main(){ while(scanf("%d%d", &n, &k)) { if(n == 0) break; for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) scanf("%d", &A.a[i][j]), A.a[i][j] %= 10; mat x = solve(k); for(int i = 1; i <= n; ++i) { for(int j = 1; j < n; ++j) printf("%d ", x.a[i][j]); printf("%d\n", x.a[i][n]); } puts(""); } return 0;}
View Code

 

转载于:https://www.cnblogs.com/19992147orz/p/6821935.html

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
Linux下Nginx安装
查看>>
LVM扩容之xfs文件系统
查看>>
Hbase记录-client访问zookeeper大量断开以及参数调优分析(转载)
查看>>
代码片段收集
查看>>
vue-cli3创建项目时报错
查看>>
输入1-53周,输出1-53周的开始时间和结束时间
查看>>
实验二
查看>>
shell——按指定列排序
查看>>
crash 收集
查看>>
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>