题目传送门

题意:f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d,求f (n) % m。训练指南的题目

分析:令:.则

#include <bits/stdc++.h>

int d, n, m;
int a[16], f[16]; struct Mat {
int m[17][17];
int row, col;
Mat() {
//row = col = 16;
memset (m, 0, sizeof (m));
}
void init(int sz) {
row = col = sz;
for (int i=1; i<row; ++i) {
m[i][i+1] = 1;
}
int c = sz - 1;
for (int i=2; i<=col; ++i) {
m[sz][i] = a[c--];
}
}
void change(int sz) {
row = col = sz;
for (int i=1; i<=sz; ++i) {
m[i][i] = 1;
}
}
}; Mat operator * (const Mat &a, const Mat &b) {
Mat ret;
ret.row = a.row; ret.col = b.col;
for (int i=1; i<=a.row; ++i) {
for (int j=1; j<=b.col; ++j) {
for (int k=1; k<=a.col; ++k) {
int &r = ret.m[i][j];
r = (r + 1ll * a.m[i][k] * b.m[k][j]) % m;
}
}
}
return ret;
} Mat operator ^ (Mat x, int n) {
Mat ret; ret.change (d+1);
while (n) {
if (n & 1) {
ret = ret * x;
}
x = x * x; n >>= 1;
}
return ret;
} //Running_Time
int main() {
while (scanf ("%d%d%d", &d, &n, &m) == 3) {
if (!d && !n && !m) {
break;
}
for (int i=1; i<=d; ++i) {
scanf ("%d", a+i);
}
for (int i=1; i<=d; ++i) {
scanf ("%d", f+i);
}
if (n <= d) {
printf ("%d\n", f[n] % m);
} else {
Mat ans, Fd;
ans.init (d + 1);
ans = ans ^ (n - d); Fd.row = d + 1; Fd.col = 1;
for (int i=2; i<=d+1; ++i) {
Fd.m[i][1] = f[i-1];
} ans = ans * Fd;
printf ("%d\n", ans.m[d+1][1]);
}
} return 0;
}

  

矩阵快速幂 UVA 10870 Recurrences的更多相关文章

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

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

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

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

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

  4. 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) ...

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

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

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

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

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

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

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

  9. UVA10870 Recurrences —— 矩阵快速幂

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

随机推荐

  1. 地图API文档

    目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...

  2. ios WaxPatch热更新原理

    以下是引用他人文章内容: 为什么需要 WaxPatch 很多情况下,已经在 AppStore 上线的应用需要紧急缺陷修复,此时便需要使用某些技术手段,使应用程序能够动态下载补丁,进行缺陷修复. 什么是 ...

  3. UbuntuLinux安装java

    jdk1.7,jdk1.8详情,参见:http://www.cnblogs.com/a2211009/p/4265225.html

  4. hdu2030 汉字统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2030 解题思路:主要考察汉字的编码方式, 汉字机内码在计算机的表达方式的描述是,使用二个字节,汉字的每 ...

  5. mysqlbinlog 查看日志时发生报错

    [root@cs Downloads]# mysqlbinlog mysql-bin. ERROR: Error , event_type: ERROR: Could not read entry a ...

  6. 【转载】PHP使用1个crontab管理多个crontab任务

    转载来自: http://www.cnblogs.com/showker/archive/2013/09/01/3294279.html http://www.binarytides.com/php- ...

  7. My97DatePicker使用技巧

    My97DatePicker使用是很常用的控件,总结一下常用使用技巧: 1.onpicked是事件,也就选择日期之后触发事件: 2.isShowClear:是否显示清理按钮: 3.maxDate:最大 ...

  8. gdo图形引擎中的旋转角

    横滚角(Roll) bank.roll  绕y轴 z轴正向为起点逆时针方向:往左为正,往右为负,水平时为0:有效范围:-180度-180度 注:下图是从飞机的尾部-->头部方向观察所得 俯仰角( ...

  9. Linux中exec()执行文件系列函数的使用说明

    函数原型: 描述:    exec()系列函数使用新的进程映像替换当前进程映像.    工作方式没有什么差别, 只是参数传递的方式不同罢了. 说明:    1. 这6个函数可分为两大类: execl( ...

  10. PowerDesigner生成sql及HTML格式数据库文档

    一.PowerDesigner生成sql问题 生成sql的方法是 Database -->Generate Database (Ctrl + G ) 但是提示 Could not load VB ...