HDU5015 233 Matrix —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5015
233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2805 Accepted Submission(s): 1611
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
1
2 2
0 0
3 7
23 47 16
2799
72937
题解:
假设n = 4,则矩阵中第0列元素为:
a[0][0]
a[1][0]
a[2][0]
a[3][0]
a[4][0]
根据递推,第1列为:
a[0][1] = a[0][1]
a[1][1] = a[0][1] + a[1][0]
a[2][1] = a[0][1] + a[1][0] + a[2][0]
a[3][1] = a[0][1] + a[1][0] + a[2][0] + a[3][0]
a[4][1] = a[0][1] + a[1][0] + a[2][0] + a[3][0] + a[4][0]
第m列为:
a[0][m] = a[0][m]
a[1][m] = a[0][m] + a[1][m-1]
a[2][m] = a[0][m] + a[1][m-1] + a[2][m-1]
a[3][m] = a[0][m] + a[1][m-1] + a[2][m-1] + a[3][m-1]
a[4][m] = a[0][m] + a[1][m-1] + a[2][m-1] + a[3][m-1]+ a[4][m-1]
可发现当前一列可直接由上一列递推出来,因此构造矩阵:

代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = ;
const int MAXN = 1e6+; const int Size = ;
struct MA
{
LL mat[][];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA mul(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%MOD, ret.mat[i][j] %= MOD;
return ret;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
} int main()
{
LL n, m, a[];
while(scanf("%lld%lld",&n,&m)!=EOF)
{ for(int i = ; i<=n; i++)
scanf("%lld", &a[i]);
a[] = ; a[n+] = ; MA s;
memset(s.mat, , sizeof(s.mat));
for(int i = ; i<=n; i++)
{
s.mat[i][] = ;
s.mat[i][n+] = ;
for(int j = ; j<=i; j++)
s.mat[i][j] = ;
}
s.mat[n+][n+] = ; s = qpow(s, m);
LL ans = ;
for(int i = ; i<=n+; i++)
ans += 1LL*a[i]*s.mat[n][i]%MOD, ans %= MOD; printf("%lld\n", ans);
}
}
HDU5015 233 Matrix —— 矩阵快速幂的更多相关文章
- HDU5015 233 Matrix(矩阵高速幂)
HDU5015 233 Matrix(矩阵高速幂) 题目链接 题目大意: 给出n∗m矩阵,给出第一行a01, a02, a03 ...a0m (各自是233, 2333, 23333...), 再给定 ...
- 233 Matrix 矩阵快速幂
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- HDU - 5015 233 Matrix (矩阵快速幂)
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- 233 Matrix(矩阵快速幂+思维)
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- HDU 5015 233 Matrix --矩阵快速幂
题意:给出矩阵的第0行(233,2333,23333,...)和第0列a1,a2,...an(n<=10,m<=10^9),给出式子: A[i][j] = A[i-1][j] + A[i] ...
- fzu 1911 Construct a Matrix(矩阵快速幂+规律)
题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...
- UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)
题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...
- UVa 11149 Power of Matrix 矩阵快速幂
题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\ ...
- Construct a Matrix (矩阵快速幂+构造)
There is a set of matrixes that are constructed subject to the following constraints: 1. The matrix ...
随机推荐
- 树(弱化版)(lca)
3306: 树 时间限制: 10 Sec 内存限制: 256 MB 题目描述 给定一棵大小为 n 的有根点权树,支持以下操作: • 换根 • 修改点权 • 查询子树最小值 输入 第一行 ...
- JDK1.8和Spring 3.2.0 的坑
上午 org.apache.catalina.core.StandardContext listenerStart严重: Exception sending context initialized e ...
- SPOJ 8222 Substrings
题面 Description 给长度为 n 的字符串 S , 对任意的 L , 求长度为 L 的子串最多出现的次数. Input String S consists of at most 250000 ...
- Java生成GUID的方法
其实在Java上已经换了一个说法,叫做UUID,方法如下: java.util.UUID.randomUUID()
- PHP平均小数红包算法
<?php function RandMoney( $money,$num ){ $arr = array();//存放金额 $total_money = 0;//红包总金额 $thisMone ...
- 【层次查询】Hierarchical Queries之亲兄弟间的排序(ORDER SIBLINGS BY)
http://blog.itpub.net/519536/viewspace-624176 有关层次查询之前的文章参考如下. [层次查询]Hierarchical Queries之"树的遍历 ...
- php 源码编译
https://cyberpersons.com/2016/08/28/install-nginx-php-php-fpm-mysql-source-run-wordpress-site-ubuntu ...
- MySQL主从复制技术与读写分离技术amoeba应用
MySQL主从复制技术与读写分离技术amoeba应用 前言:眼下在搭建一个人才站点,估计流量会非常大,须要用到分布式数据库技术,MySQL的主从复制+读写分离技术.读写分离技术有官方的MySQL-pr ...
- WinKawaks使用技巧
1 关于载入游戏:把游戏的zip文件放到roms目录下.注意不要随意修改文件名!也不要让文件名有中文!例如:"[游戏roms][neogeo]mslugx.zip"改成" ...
- ssh bitbucket github
$ ssh-keygen -t rsa -C "mac" $ vi ~/.ssh/config Host bb User git HostName bitbucket.org Id ...