题目传送门

题意: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. 一个简单的代码计算行数demo编写

    最近手头的项目基本上已经完结,历经了5个月的开发和迭代,各种的需求调整,想对自己的代码量进行一个客观的计算,于是抽了点时间写下了这个小demo,朋友们有需要的可以看看,很简单. 基本的思想就是:根目录 ...

  2. Cannot change version of project facet Dynamic Web Module to 3.0

    背景描述: 最近在开发项目时,老是报错说:Project is not Dynamic Web Module 3.0.右击项目选择属性进行修改时出现以下错误: 这让我很是恼火,后来终于找到了万能的解决 ...

  3. September 4th 2016 Week 37th Sunday

    The morning crowned the humble cloud with splendor. 晨光为谦逊的白云披上壮丽的光彩. Humility is a virtue. Many famo ...

  4. CABasicAnimation 按home键后台之后,再切回来动画就停止了

    解决方法: 1. CABasicAnimation *thisAnimation = [CABasicAnimtaion animationWithKeyPath:@"transform.r ...

  5. ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(二) 之 ChatServer搭建,连接服务器,以及注意事项。

    上篇:ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(一) 之 基层数据搭建,让数据活起来(数据获取) 上一篇我们已经完成了初步界面的搭建工作,本篇将介绍IM的核心内容 ...

  6. 将rabbitmq整合到Spring中手动Ack

    如果要手动ack,需要将Listener container 的 acknowledge 设置为manul,在消费消息的类中需实现ChannelAwareMessageListener接口. over ...

  7. Mysql子查询

    1单值(Scalar operand) 只有当外层(Parent)不为空时,才返回相应值:否则返回NULL. note:For the subquery just shown, if t1 were ...

  8. Sample Apps by Android Team -- Amazed

    Sample Apps by Android Team 代码下载:http://pan.baidu.com/s/1eSNmdUE , 代码原地址:https://code.google.com/arc ...

  9. C#4.0图解教程 - 第24章 反射和特性 - 1.反射

    24.1 元数据和反射 有关程序及类型的数据被成为 元数据.他们保存在程序集中. 程序运行时,可以查看其他程序集或其本身的元数据.一个运行的程序查看本身元数据或其他程序的元数据的行为叫做 反射. 24 ...

  10. 重温WCF之一个服务实现多个契约(二)

    public class ServiceImp : IService1,IService2,IService3 { public string SayHelloA() { return "你 ...