题目传送门

题意: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. 高校排名 加强版(codevs 2799)

    题目描述 Description 大学排名现在已经非常流行.在网上搜索可查到关于中国大学排行的各个方面的消息. 我们知道,在一大学里通常都由许多不同的"系"(专业)组成,比如计算机 ...

  2. iOS - 定制多样式二维码

    二维码/条形码是按照某种特定的几何图形按一定规律在平台(一维/二维方向上)分布的黑白相间的图形纪录符号信息.使用若干个与二进制对应的几何形体来表示文字数值信息.   最常见的二维码功能包括信息获取.网 ...

  3. (1)第一个ASP.NET Web API

      Install-Package Microsoft.AspNet.WebApi . Global.asax protected void Application_Start() { AreaReg ...

  4. 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)

    1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  5. HTML5学习之画布和SVG(四)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. Delphi基础语法的学习笔记和注意事项总结

    以下是我在自学Delphi的时候,对一些注意点的简单总结,并没有什么系统性可言,只是一个学习时顺手记下的笔记,主要为了当时加深对知识的印象,并没有希望能在以后的复习和使用Delphi中有什么多大的参考 ...

  7. Linux(CentOS)常用操作指令(一)

    基本指令集合 1.查看CentOS版本信息 cat /proc/version cat /etc/redhat-release 2.查看安全日志文件信息 tail -f /var/log/secure ...

  8. 【翻译一】java-并发

    Lesson: Concurrency Computer users take it for granted that their systems can do more than one thing ...

  9. 【JAVA多线程中使用的方法】

    一.sleep和wait的区别. 1.wait可以指定时间,也可以不指定. 而sleep必须制定. 2.在同步的时候,对于CPU的执行权和以及锁的处理不同. wait:释放执行权,释放锁. sleep ...

  10. sdut 2441 屠夫与狼

    屠夫和狼 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...