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 ...
随机推荐
- 信号板拼包:数组方式(bug长度只是截短,并未清空,若之后拷贝数据长度小于之前数据长度,老数据会接在后面)
class SignalobardMsgReadHandler : public SessionVectChar::ReadHandler{public: SignalobardMsgReadHan ...
- 1007 Maximum Subsequence Sum
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- Java中将List转成逗号数组的方案
说明:逗号字符串转成数组或者List都是可以的,反过来依然可行:但是如果是List<String>转List<Integer>,基本误解,在Java 7只能for循环,如果在J ...
- Unity Step by Step(一)
要打败敌人,首先要了解敌人,这不是我说的,这是孙子说的.^_^ 首先,我一头雾水,所以我就下了个demo,demo会在下面附上,声明,这不是我写的,我也是下载别人的,地址:http://game.ce ...
- iOS -- 开源项目和库
TimLiu-iOS 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD与Toast 对话框 其他UI 动画 侧滑与右滑返回手势 gif动画 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 远程Event Receivers App级别生命周期
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 远程Event Receivers App级别生命周期 ...
- 【转】css浮动元素的知识
原文: http://www.cnblogs.com/xuyao100/p/8940958.html ------------------------------------------------- ...
- react 监听 移动端 手机键盘 enter 事件
处理方法: (1)html书写 form标签中去掉action参数,定义onSubmit方法,如下所示: /** * 搜索框 组件 */ import React,{PureComponent} fr ...
- vue2.0 + vux (六)NewsList 资讯页 及 NewsDetail 资讯详情页
设置代理,避免出现跨域问题 /*设置代理,避免出现跨域问题*/ proxyTable: { '/api':{ target: 'https://www.oschina.net/action/apiv2 ...
- python(36)- 测试题
1.8<<2等于? 32 “<<”位运算 264 132 64 32 16 8 4 2 1 原始位置 0 0 0 0 0 1 0 0 0 想左位移2位 0 0 0 1 0 0 ...