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 ...
随机推荐
- Android 动态生成对话框和EditText
/** * (获取输入) */ private void showInputDialog() { ScrollView scrollview = getInitView() ; final Linea ...
- 【IntelliJ IDEA】idea导入项目只显示项目中的文件,不显示项目结构
导入项目之后,只显示项目文件,不显示项目结构 解决方法 1.点击file->project structure..->Modules 点击右上角+加号 ->import Module ...
- 更新tensorflow支持GPU时出错
sudo pip install --upgrade tensorflow-gpu Operation not permitted: '/tmp/pip-Sx_vMg-uninstall/System ...
- win10 安装git
http://jingyan.baidu.com/article/a3a3f811d4cd308da2eb8ad1.html 双击exe安装包,在弹出的安全提示中点击“允许”. 安装向导的头两步都 ...
- C++ 面试问题
一面 (1) 多态性都有哪些?(静态和动态,然后分别叙述了一下虚函数和函数重载) (2) 动态绑定怎么实现?(就是问了一下基类与派生类指针和引用的转换问题) (3) 类型转换有哪些?(四种类型转换,分 ...
- hibernater-validator jar包冲突的问题
在引用hibernater-validator jar包时一直抛出异常,在引用带有该包的项目,或者同时在一个项目中使用该包和validator包都会抛出以下异常 最后发现是在Eclipse环境下,不能 ...
- python 大小写转换方法(全)
http://blog.csdn.net/liuxincumt/article/details/7945337 python大小写转换(全)
- DDR硬件设计要点详解(包括电源部分)
转自 http://www.fairchildic.org/module/forum/thread-658-1-1.html (原帖包括详细的附件内容) 1. 电源 DDR的电源可以分为三类A.主电源 ...
- 测试 MD
上面是一张图片 总店?
- JAVA传输概念
1.VO(View Object):视图对象,用于展示在前台界面. 2.DTO(Data Transfer Object):数据传输对象,泛指用于展示层与服务层之间的数据传输对象. 3. DTO和VO ...