233 Matrix 矩阵快速幂
InputThere are multiple test cases. Please process till EOF.
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).OutputFor each case, output a n,m mod 10000007.Sample Input
1 1
1
2 2
0 0
3 7
23 47 16
Sample Output
234
2799
72937
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 18
#define N 33
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0);
/*
组合数学 找规律
递归显然不行,列数太多
只需考虑每个点被加上的次数
a(i,0) = a(i,1) 到 a(n,m) 路径条数(向左和向下两个方向) C(n+m-i-1,n)
发现列数太多没办法打表 再换一种方法
矩阵快速幂
从第一列向后考虑 找出他们的转移矩阵(这里很巧妙的加了一条边 凑2333后面的3)十分巧妙!~
*/
LL a[MAXN], n, m;
struct mat
{
LL data[MAXN][MAXN];
mat()
{
memset(data, , sizeof(data));
}
mat operator*(const mat& rhs)
{
mat ret;
for (int i = ; i <= n + ; i++)
{
for (int j = ; j <= n + ; j++)
{
for (int k = ; k <= n + ; k++)
ret.data[i][j] = (ret.data[i][j] + data[i][k] * rhs.data[k][j]) % MOD;
}
}
return ret;
}
};
mat fpow(mat a, LL b)
{
if (b <= ) return a;
mat tmp = a, ret;
for (int i = ; i <= n + ; i++)
ret.data[i][i] = ;
while (b!= )
{
if (b & )
ret = tmp*ret;
tmp = tmp*tmp;
b = b / ;
}
return ret;
}
int main()
{
while (cin >> n >> m)
{
a[] = ;
for (int i = ; i <= n + ; i++)
cin >> a[i];
a[n + ] = ;
mat ans;
for (int i = ; i <= n + ; i++)
{
ans.data[i][] = ;
ans.data[i][n + ] = ;
for (int j = ; j <= i; j++)
ans.data[i][j] = ;
}
ans.data[n + ][n + ] = ;
ans = fpow(ans, m);
LL result = ;
for (int i = ; i <= n + ; i++)
result = (result + a[i] * ans.data[n + ][i]) % MOD;
cout << result << endl;
}
return ;
}
233 Matrix 矩阵快速幂的更多相关文章
- 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 ...
- HDU5015 233 Matrix —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5015 233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memor ...
- 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] ...
- HDU5015 233 Matrix(矩阵高速幂)
HDU5015 233 Matrix(矩阵高速幂) 题目链接 题目大意: 给出n∗m矩阵,给出第一行a01, a02, a03 ...a0m (各自是233, 2333, 23333...), 再给定 ...
- 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 ...
随机推荐
- 路一直都在——That's just life
分享一首很喜欢的歌,有时候歌词写得就是经历,就是人生... 穿过人潮汹涌灯火栏栅 没有想过回头 一段又一段走不完的旅程 什么时候能走完 我的梦代表什么 又是什么让我们不安 That's just li ...
- Euclid(几何)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2831 题意:已知A,B,C,D,E,F的坐标, ...
- HTML--使用单选框、复选框,让用户选择
在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好主意,html中有两种选择框,即单选框和复选框,两者的区别是单选框中的选项用户只能选择一项,而复选框中用户可以任意选择多项,甚至全选.请看 ...
- 321 Create Maximum Number 拼接最大数
已知长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,直观地表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中 ...
- netty学习:UDP服务器与Spring整合
最近接到一个关于写UDP服务器的任务,然后去netty官网下载了netty的jar包(netty-4.0.49.Final.tar.bz2),解压后,可以看到上面有不少example,找到其中的关于U ...
- Python3之Zip
from collections import defaultdict from collections import OrderedDict d = defaultdict(list) d['a'] ...
- html——特例
1.a标签与a标签之间有3px距离 2.标准流中的文字不会被浮动的盒子遮挡 <div style="width:150px;height:150px;background-color: ...
- 易买网之smartupload实现文件上传
经过俩个星期的奋斗,易买网项目完工.在之前,实现图片的上传,走过许多弯路,原来是好多基础的知识忘记了,没把smartupload文件包添加组件jar包至WEB-INF/lib包中,在此特别重视,做下文 ...
- 3星|《IBM商业价值报告:区块链》:一些重要行业对区块链的态度和已经发生的区块链的应用
区块链项目开发指南 (区块链技术丛书) 介绍IBM的专家们调研许多重要行业与组织后总结的各行业对区块链的态度和实际的应用.看起来有点意思,不过有两个缺点: 1:这些实际已经发生的应用基本没看到相关的新 ...
- css知识框架