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的更多相关文章

  1. UVA 10870 - Recurrences(矩阵高速功率)

    UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...

  2. 矩阵快速幂 UVA 10870 Recurrences

    题目传送门 题意:f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d,求f (n) % m.训 ...

  3. 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 ...

  4. UVa 10870 Recurrences (矩阵快速幂)

    题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...

  5. UVA - 10870 Recurrences 【矩阵快速幂】

    题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...

  6. UVa 10870 (矩阵快速幂) Recurrences

    给出一个d阶线性递推关系,求f(n) mod m的值. , 求出An-dv0,该向量的最后一个元素就是所求. #include <iostream> #include <cstdio ...

  7. Recurrences UVA - 10870 (斐波拉契的一般形式推广)

    题意:f(n) = a1f(n−1) + a2f(n−2) + a3f(n−3) + ... + adf(n−d), 计算这个f(n) 最重要的是推出矩阵. #include<cstdio> ...

  8. uva 10870 递推关系矩阵快速幂模

    Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...

  9. UVA - 10870 UVA - 10870

    Problem ARecurrencesInput: standard inputOutput: standard output Consider recurrent functions of the ...

随机推荐

  1. maven核心概念4

    一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识(包名) artifactId:项目名称 ver ...

  2. Java中的数学运算BigDecimal

    Math类 package ch7; /** * Created by Jiqing on 2016/11/24. */ public class MathDemo { public static v ...

  3. TCP及IP报头及协议

    看到有道题目要问:tcp头多少字节?哪些字段?(必问) 这个... 看了这篇文章做参考:http://blog.163.com/tianshuai11@126/blog/static/61894543 ...

  4. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  5. 扩展spring data jpa的数据更新方法时注意事项

    //此处必须加@Transactional,否则不能运行,报错 @Transactional @Modifying @Query("update ExamItem a set a.versi ...

  6. phalcon(费尔康)框架学习笔记

    phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构   pha ...

  7. toolbar

    toolbar= (Toolbar) findViewById(R.id.toolbar_mainactivity); // 设置主标题及其颜色 toolbar.setTitle("Andr ...

  8. VB6 GDI+ 入门教程[9] Bitmap魔法(2):数据读写

    本文转自 http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[9] Bitmap魔法(2):数据读写 200 ...

  9. 解决PhoneGap不支持viewport的几种方法

    前几天用phonegap编译GameBuilder+CanTK时,发现HTML里的viewport无效.CanTK根据devicePixelRatio检测设备的DPI,然后用viewport设置正确的 ...

  10. eclipse 新建 maven 项目 步骤(初级入门新手)

    安装 maven(百度) 和在eclipse 中svn(上一篇) 修改 maven 本地仓库 eclipse 属性 maven--> installations-->添加新的 自定义安装的 ...