UVa 10870 - Recurrences
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1811
矩阵快速幂
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<stdexcept>
#include<bitset>
#include<cassert>
#include<deque> using namespace std; typedef long long ll;
typedef unsigned int uint;
const double eps=1e-12;
const int INF=0x3f3f3f3f;
const int N=50;
ll ma[N][N],mb[N][N],mc[N][N];
void matrixMul(ll a[][N],ll b[][N],int n,int m,int k,ll MOD)
{
memset(mc,0,sizeof(mc));
for(int i=1;i<=n;++i)
for(int j=1;j<=k;++j)
for(int l=1;l<=m;++l)
mc[i][j]=(mc[i][j]+a[i][l]*b[l][j]%MOD)%MOD;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
a[i][j]=mc[i][j];
}
int main()
{
//freopen("data.in","r",stdin);
int d,n;
ll m;
while(cin>>d>>n>>m)
{
if(!d&&!n&&!m) break;
memset(mb,0,sizeof(mb));
for(int i=1;i<=d;++i)
cin>>mb[i][1];
for(int i=2;i<=d;++i)
mb[i-1][i]=1;
for(int i=d;i>=1;--i)
cin>>ma[1][i];
if(n<=d)
{
cout<<ma[1][d+1-n]<<endl;
continue;
}
n-=d;
while(n)
{
if((n&1))
matrixMul(ma,mb,1,d,d,m);
n=n>>1;
matrixMul(mb,mb,d,d,d,m);
}
cout<<ma[1][1]<<endl;
}
return 0;
}
UVa 10870 - Recurrences的更多相关文章
- UVA 10870 - Recurrences(矩阵高速功率)
UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...
- 矩阵快速幂 UVA 10870 Recurrences
题目传送门 题意:f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d,求f (n) % m.训 ...
- UVA 10870 Recurrences(矩阵乘法)
题意 求解递推式 \(f(n)=a_1*f(n-1)+a_2*f(n-2)+....+a_d*f(n-d)\) 的第 \(n\) 项模以 \(m\). \(1 \leq n \leq 2^{31}-1 ...
- UVa 10870 Recurrences (矩阵快速幂)
题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...
- UVA - 10870 Recurrences 【矩阵快速幂】
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...
- UVa 10870 (矩阵快速幂) Recurrences
给出一个d阶线性递推关系,求f(n) mod m的值. , 求出An-dv0,该向量的最后一个元素就是所求. #include <iostream> #include <cstdio ...
- Recurrences UVA - 10870 (斐波拉契的一般形式推广)
题意:f(n) = a1f(n−1) + a2f(n−2) + a3f(n−3) + ... + adf(n−d), 计算这个f(n) 最重要的是推出矩阵. #include<cstdio> ...
- uva 10870 递推关系矩阵快速幂模
Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...
- UVA - 10870 UVA - 10870
Problem ARecurrencesInput: standard inputOutput: standard output Consider recurrent functions of the ...
随机推荐
- mysql 理解 int(11)
1.这里的int(11) 与int的大小和存储字节,没有一毛钱关系,int的存储字节是4个字节,最大值为 65536*65536 = 40多亿,对于有符号的int,是20多亿.2.那么这里的(11) ...
- (二)程序中的内存&&栈
一.程序运行为什么需要内存?基本概念? 内存是程序运行的立足之地,程序需要用内存来存储一些变量. 内存管理最终是由操作系统完成的,内存在本质上是一个硬件器件,由硬件系统提供:内存由操作系统统一管理,为 ...
- [css] line boxes
原文链接:http://www.zhangxinxu.com/wordpress/2010/01/css-float%E6%B5%AE%E5%8A%A8%E7%9A%84%E6%B7%B1%E5%85 ...
- 转:strcpy实现的考察要点
strcpy函数的实现 已知strcpy函数的原型是: char *strcpy(char *dst, const char *src); 实现strcpy函数 解释为什么要返回char * 假如考虑 ...
- spring+redis实现缓存
spring + redis 实现数据的缓存 1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个j ...
- Vbs脚本经典教材(转)
Vbs脚本经典教材(最全的资料还是MSDN) —为什么要使用Vbs? 在Windows中,学习计算机操作也许很简单,但是很多计算机工作是重复性劳动,例如你每周也许需要对一些计算机文件进行复制.粘贴.改 ...
- listToString
http://www.oschina.net/code/snippet_109648_2229
- phalcon(费尔康)框架学习笔记
phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构 pha ...
- js中Math.random()生成指定范围数值的随机数
http://www.111cn.net/wy/js-ajax/57062.htm Math.random() 这个方法相信大家都知道,是用来生成随机数的.不过一般的参考手册时却没有说明如何用这个方法 ...
- 集成骨骼动画Spine的几点经验
最近开始用cantk做些复杂的游戏,其中一个游戏的DragonBones骨骼动画的JSON文件就达600K,导出之后显示各种不正常,可能是太复杂了,有些方面达到了DragonBones的极限.拿到官方 ...