题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340)。a1 , a2 ..... ad。f(1), f(2) ..... f(d),求 f(n) = a1*f(n-1) + a2*f(n-2) +....+ ad*f(n-d),计算f(n) % m。

析:很明显的矩阵快速幂,构造矩阵,

,然后后面的就很简单了。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #include <numeric>
  21. #define debug() puts("++++")
  22. #define gcd(a, b) __gcd(a, b)
  23. #define lson l,m,rt<<1
  24. #define rson m+1,r,rt<<1|1
  25. #define fi first
  26. #define se second
  27. #define pb push_back
  28. #define sqr(x) ((x)*(x))
  29. #define ms(a,b) memset(a, b, sizeof a)
  30. #define sz size()
  31. #define pu push_up
  32. #define pd push_down
  33. #define cl clear()
  34. #define lowbit(x) -x&x
  35. //#define all 1,n,1
  36. #define FOR(i,n,x) for(int i = (x); i < (n); ++i)
  37. #define freopenr freopen("in.in", "r", stdin)
  38. #define freopenw freopen("out.out", "w", stdout)
  39. using namespace std;
  40.  
  41. typedef long long LL;
  42. typedef unsigned long long ULL;
  43. typedef pair<int, int> P;
  44. const int INF = 0x3f3f3f3f;
  45. const LL LNF = 1e17;
  46. const double inf = 1e20;
  47. const double PI = acos(-1.0);
  48. const double eps = 1e-8;
  49. const int maxn = 20 + 10;
  50. const int maxm = 1e6 + 2;
  51. const LL mod = 1000000007;
  52. const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
  53. const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
  54. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  55. int n, m;
  56. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  57. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  58. inline bool is_in(int r, int c) {
  59. return r >= 0 && r < n && c >= 0 && c < m;
  60. }
  61.  
  62. struct Matrix{
  63. int a[15][15], n;
  64. void init(){ ms(a, 0); }
  65. void toOne(){ FOR(i, n, 0) a[i][i] = 1; }
  66. Matrix operator * (const Matrix &rhs){
  67. Matrix res; res.n = n; res.init();
  68. FOR(i, n, 0) FOR(j, n, 0) FOR(k, n, 0)
  69. res.a[i][j] = (res.a[i][j] + (LL)a[i][k] * rhs.a[k][j]) % m;
  70. return res;
  71. }
  72. };
  73.  
  74. Matrix fast_pow(Matrix x, int n){
  75. Matrix res; res.n = x.n; res.init(); res.toOne();
  76. while(n){
  77. if(n&1) res = res * x;
  78. x = x * x;
  79. n >>= 1;
  80. }
  81. return res;
  82. }
  83.  
  84. int main(){
  85. int d;
  86. while(scanf("%d %d %d", &d, &n, &m) == 3 && n+m+d){
  87. Matrix x, y; x.init(); y.init();
  88. x.n = y.n = d;
  89. for(int i = 0; i < d; ++i){
  90. scanf("%d", &y.a[i][0]);
  91. y.a[i][0] %= m;
  92. }
  93. for(int i = d-1; i >= 0; --i){
  94. scanf("%d", &x.a[0][i]);
  95. x.a[0][i] %= m;
  96. }
  97. if(n <= d){ printf("%d\n", x.a[0][d-n]); continue; }
  98. for(int i = 0; i + 1 < d; ++i) y.a[i][i+1] = 1;
  99. Matrix ans = x * fast_pow(y, n - d);
  100. printf("%d\n", ans.a[0][0]);
  101. }
  102. return 0;
  103. }

  

UVa 10870 Recurrences (矩阵快速幂)的更多相关文章

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

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

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

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

  3. UVA10870 Recurrences —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...

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

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

  5. POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】

    典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...

  6. uva 10518 - How Many Calls?(矩阵快速幂)

    题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...

  7. Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)

    题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...

  8. UVA - 11149 (矩阵快速幂+倍增法)

    第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio ...

  9. UVA10870—Recurrences(简单矩阵快速幂)

    题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...

随机推荐

  1. C#字符串长度判断

    string aaa = "你好123"; Label1.Text = aaa.Length.ToString();  //结果5 Label2.Text = System.Tex ...

  2. GridView和DataFormatString 日期格式 精确小数点后位数

    如果DataFormatString无效,请添加属性 HtmlEncode = "false" --------------------------------------- Da ...

  3. Xcode 去掉控制台无用打印信息

    1. 2.在Environment Variables增加一键值对 OS_ACTIVITY_MODE = disable 转自:https://blog.csdn.net/HelloWorld_198 ...

  4. Java 枚举类 详解

    1.枚举是什么? Java中的枚举其实是一种语法糖,在 JDK 1.5之后出现,用来表示固定且有限个的对象.比如一个季节类有春.夏.秋.冬四个对象:一个星期有星期一到星期日七个对象.这些明显都是固定的 ...

  5. webpack浅析---入口篇

    webpack有四个核心概念: 入口(entry) 输出(output) loader 插件(plugins) webpack-merge将环境.构建目标.运行时合并 入口: 入口起点是指webpac ...

  6. springmvc中的拦截器interceptor用法

    1.配置拦截器 在springMVC.xml配置文件增加: 1 <mvc:interceptors> 2 <!-- 日志拦截器 --> 3 <mvc:intercepto ...

  7. XSS 攻击的防御

    xss攻击预防,网上有很多介绍,发现很多都是只能预防GET方式请求的xss攻击,并不能预防POST方式的xss攻击.主要是由于POST方式的参数只能用流的方式读取,且只能读取一次,经过多次尝试,自己总 ...

  8. python tcp 粘包问题解决、文件下载等

    from socket import * #以下是关于tcp:服务端 和 客户端的小例子#服务端socket_server = socket(AF_INET, SOCK_STREAM) socket_ ...

  9. Ajax cookie session form组件

    . Cookie是什么 保存在浏览器端的键值对 为什么要有Cookie? 因为HTTP请求是无状态的 Cookie的原理? 服务端可以在返回响应的时候 做手脚 在浏览器上写入键值对(Cookie) 浏 ...

  10. 5.Mysql常用函数

    5.常用函数函数可以进行字符串的处理.数值计算和日期计算等,mysql可以用在SQL(DML)中以增加SQL的功能.5.1 数值函数1. abs(x) 返回x的绝对值select abs(5),abs ...