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 文中用(比 ...
随机推荐
- Cropping multiple images the same way
The tools we’ll be using are =GIMP= and =mogrify= (from the ImageMagick suite), so make sure that yo ...
- VS2015 scanf用不了
#define _CRT_SECURE_NO_DEPRECATE
- 如何使用JDK1.6 API
如何使用JDK1.6 API-------https://jingyan.baidu.com/article/54b6b9c0e39a102d583b47d5.html
- noip模拟赛 轮换
分析:模拟题,关键就是要理解题目意思.m≥3的轮换可以拆成m=2的小轮换,小轮换的话只需要交换一下就可以了. #include <cstdio> #include <cstring& ...
- P1072 Hankson的趣味题
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- Just a Hook-HDU1698(线段树求区间)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem Description In the game of DotA, Pudge’s meat ...
- 利用try-catch代码检查用户输入数据是否是有效的浮点数超级详细
package chapter6; //数据输入格式检查 import java.io.IOException; import java.util.InputMismatchException; im ...
- NOIP 2010 机器翻译
P1540 机器翻译 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于 ...
- Ubuntu 16.04安装TeamViewer
安装i386库: sudo apt-get install libc6:i386 libgcc1:i386 libasound2:i386 libexpat1:i386 libfontconfig1: ...
- [Jest] Automate your migration to Jest using codemods
Jest is a fantastic testing library, but maybe you've been putting off the switch because migrating ...