矩阵快速幂 UVA 10870 Recurrences
题意: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的更多相关文章
- 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(矩阵高速功率)
UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...
- 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) ...
- uva 10518 - How Many Calls?(矩阵快速幂)
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...
- 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 ...
- UVA - 11149 (矩阵快速幂+倍增法)
第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio ...
- UVA10870—Recurrences(简单矩阵快速幂)
题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...
- UVA10870 Recurrences —— 矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...
随机推荐
- Jquery 点击按钮将其背景图换成另一张,再次点击恢复默认图片
这是Jquery代码: $(function () { $("#h1").toggle(function () { $("#h1").css("bac ...
- Android笔记:ListView
listview属性 android:divider属性,可以指定ListView 分隔线的颜色,#0000 表示将分隔线设为透明色. listview效率的问题 adapter的三个参数int po ...
- Vi文档
Vi简介 Vi是一种广泛存在于各种UNIX和Linux系统中的文本编辑程序. Vi不是排版程序,只是一个纯粹的文本编辑程序. Vi是全屏幕文本编辑器,它没有菜单,只有命令. Vi不是基于窗口的,所以, ...
- 网页(HTML)中的特殊字符
网页(HTML)中的特殊字符 (1)一般来说,在HTML中,一个特殊字符有两种表达方式,一种称作数字参考,一种称作实体参考. 所谓数字参考,就是用数字来表示文档中的特殊字符,通常由前缀“&#” ...
- 自定义UIDatePikerView
1.添加文件GoYearMonthDayPickerView.h .m .xib.NSDate+Helper.h .m.iCarousel.h .m 2.在Lable上显示日期 UILabel *ag ...
- Android xml text 预览属性
只在 AS 中生效 xmlns:tools="http://schemas.android.com/tools" tools:text="I am a title&quo ...
- 直接放个DB2 SQL STATEMENT大全好了!
SQL statements This topic contains tables that list the SQL statements classified by type. SQL sch ...
- NYOJ题目113字符串替换
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr4AAAHvCAIAAAA930vtAAAgAElEQVR4nO3dPVLjysIG4G8T5CyE2A ...
- 一个简单的jsp+servlet实例,实现简单的登录
开发环境myeclipse+tomcat6 1.先创建web project,项目名为RegisterSystem, 2.在WebRoot 目录下创建login.jsp文件,只需修改body中的内容, ...
- js获取缓存数据
后台:request.setAttribute("type", type); 前台js获取:var type = "${type}";