http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1811

矩阵快速幂

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<set>
  7. #include<map>
  8. #include<stack>
  9. #include<vector>
  10. #include<algorithm>
  11. #include<queue>
  12. #include<stdexcept>
  13. #include<bitset>
  14. #include<cassert>
  15. #include<deque>
  16.  
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef unsigned int uint;
  21. const double eps=1e-12;
  22. const int INF=0x3f3f3f3f;
  23. const int N=50;
  24. ll ma[N][N],mb[N][N],mc[N][N];
  25. void matrixMul(ll a[][N],ll b[][N],int n,int m,int k,ll MOD)
  26. {
  27. memset(mc,0,sizeof(mc));
  28. for(int i=1;i<=n;++i)
  29. for(int j=1;j<=k;++j)
  30. for(int l=1;l<=m;++l)
  31. mc[i][j]=(mc[i][j]+a[i][l]*b[l][j]%MOD)%MOD;
  32. for(int i=1;i<=n;++i)
  33. for(int j=1;j<=m;++j)
  34. a[i][j]=mc[i][j];
  35. }
  36. int main()
  37. {
  38. //freopen("data.in","r",stdin);
  39. int d,n;
  40. ll m;
  41. while(cin>>d>>n>>m)
  42. {
  43. if(!d&&!n&&!m) break;
  44. memset(mb,0,sizeof(mb));
  45. for(int i=1;i<=d;++i)
  46. cin>>mb[i][1];
  47. for(int i=2;i<=d;++i)
  48. mb[i-1][i]=1;
  49. for(int i=d;i>=1;--i)
  50. cin>>ma[1][i];
  51. if(n<=d)
  52. {
  53. cout<<ma[1][d+1-n]<<endl;
  54. continue;
  55. }
  56. n-=d;
  57. while(n)
  58. {
  59. if((n&1))
  60. matrixMul(ma,mb,1,d,d,m);
  61. n=n>>1;
  62. matrixMul(mb,mb,d,d,d,m);
  63. }
  64. cout<<ma[1][1]<<endl;
  65. }
  66. return 0;
  67. }

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. js object(对象)

    http://www.cnblogs.com/pingchuanxin/p/5773326.html Object(对象)是在所有的编程语言中都十分重要的一个概念,对于事物我们可以把他们看作是一个对象 ...

  2. golang时间

    //获取本地location toBeCharge := "2015-01-01 00:00:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板 ...

  3. easyui 进度条

    进度条创建 $.messager.progress({ title:'请稍后', msg:'正在努力...' }); 进度条关闭 $.messager.progress('close'); 弹窗对话框 ...

  4. ZendStudio的配置导出

    File(文件)->Export(导 出),再弹出Export窗口中点击"General(常规)",选择"Preferences(首选项)" 点击&quo ...

  5. openfire升级指南

    原文:http://www.liuhaihua.cn/archives/355.html 升级Openfire是和从头开始安装Openfire几乎一样简单.作为升级过程的一部分,它强烈建议您先备份当前 ...

  6. c++ char * const p问题

    事实上这个概念谁都有,只是三种声明方式非常相似很容易记混. Bjarne在他的The C++ Programming Language里面给出过一个助记的方法: 把一个声明从右向左读. char * ...

  7. 记录---base64

    什么是Base64呢? Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045-RFC2049,上面有MIME的详细规范.Base64编码可用于在HTTP环境下 ...

  8. Css_Backgroud-position(背景图片)定位问题详解

    background-position的说明:    设置或检索对象的背景图像位置.必须先指定 background-image 属性.该属性定位不受对象的补丁属性( padding )设置影响.   ...

  9. mysql 保留两位小数

    mysql保留字段小数点后两位小数用函数:truncate(s.price,2)即可.如果想用四舍五入的话用round(s.price,2).  

  10. VC++编译GSL

    目录 第1章 VC++    1 1.1 修改行结束符    1 1.2 修改#include "*.c" 为 #include "*.inl"    2 1. ...