uva11149
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的更多相关文章
- UVA11149 Power of Matrix —— 矩阵倍增、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-11149 题意: 给出矩阵A,求出A^1 + A^2 …… + A^k . 题解: 1.可知:A^1 + A^2 …… + A ...
- 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 ...
- Power of Matrix(uva11149+矩阵快速幂)
Power of Matrix Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- 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) ...
- uva11149矩阵快速幂
求A+A^1+...+A^n 转换一下变成|A E|,的n+1次方就是|A^(n+1) A^n+...+A+E| |0 E| | 0 ...
- UVa 11149 矩阵的幂(矩阵倍增法模板题)
https://vjudge.net/problem/UVA-11149 题意: 输入一个n×n矩阵A,计算A+A^2+A^3+...A^k的值. 思路: 矩阵倍增法. 处理方法如下,一直化简下去直到 ...
- HDU2243 考研路茫茫——单词情结 ——AC自动机、矩阵优化
题目链接:https://vjudge.net/problem/HDU-2243 考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others) Memor ...
- 矩阵乘法优化DP复习
前言 最近做毒瘤做多了--联赛难度的东西也该复习复习了. Warning:本文较长,难度分界线在"中场休息"部分,如果只想看普及难度的可以从第五部分直接到注意事项qwq 文中用(比 ...
随机推荐
- 爬虫框架urllib 之(二) --- urllib基础
urllib 官方文档:https://docs.python.org/zh-cn/3/library/urllib.html urllib介绍 Urllib是python内置的HTTP请求库,是py ...
- pandas的合并、连接、去重、替换
import pandas as pd import numpy as np # merge合并 ,类似于Excel中的vlookup df1 = pd.DataFrame({'key': ['K0' ...
- Spider-scrapy断点续爬
scrapy的每一个爬虫,暂停时可以记录暂停状态以及爬取了哪些url,重启时可以从暂停状态开始爬取过的URL不在爬取 实现暂停与重启记录状态 方法一: 1.首先cd进入到scrapy项目里(当然你也可 ...
- Linux下安装SVN,仓库创建,用户权限管理
Exported from Notepad++ Linux下安装SVN,仓库创建,用户权限管理 1.SVN安装 Ubuntu系统下安装:sudoapt-getinstallsubv ...
- ZOJ 2567 Trade
Trade Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 2567 ...
- 全文搜索(A-3)-用户建模
用户模型可以分为静态模型.动态模型.混合推荐用户模型. 静态模型往往通过显式方式收集用户偏好信息: 动态模型通过隐式方式收集用户偏好信息: 基于内容的用户系统的推荐模型: 关键字匹配,空间向量模型 协 ...
- linux 常见名词及命令(三)
tar 用于对文件打包压缩或解压. 示例: 打包并压缩文件:tar -czvf 压缩包名.tar.gz 文件名 解压并展开压缩包:tar -zxvf 压缩包名.tar.gz -c 创建压缩文件 -x ...
- openOffice word转pdf,pdf转图片优化版
之前写了一个版本的,不过代码繁琐而且不好用,效率有些问题.尤其pdf转图片速度太慢.下面是优化版本的代码. spriing_boot 版本信息:2.0.1.RELEASE 1.配置信息: packag ...
- 18.9.23 PION模拟赛
U32670 小凯的数字 题目背景 NOIP2018 原创模拟题T1 NOIP DAY1 T1 or DAY 2 T1 难度 是否发现与NOIP2017 DAY1 T1 有异曲同工之妙 说明:#10, ...
- NOIP 2010 关押罪犯
P1525 关押罪犯 题目描述 SS 城现有两座监狱,一共关押着 NN 名罪犯,编号分别为 1-N1−N .他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突. ...