uva10870 递推关系Recurrences
Consider recurrent functions of the following form:
f(n) = a1f(n - 1) + a2f(n - 2) + a3f(n - 3) + : : : + adf(n - d); for n > d;
where a1, a2, …, ad are arbitrary constants.
A famous example is the Fibonacci sequence, defned as: f(1) = 1, f(2) = 1, f(n) = f(n - 1) +
f(n - 2). Here d = 2, a1 = 1, a2 = 1.
Every such function is completely described by specifying d (which is called the order of recurrence),
values of d coefcients: a1, a2, …, ad, and values of f(1), f(2), …, f(d). You’ll be given these numbers,
and two integers n and m. Your program’s job is to compute f(n) modulo m.
Input
Input fle contains several test cases. Each test case begins with three integers: d, n, m, followed by
two sets of d non-negative integers. The frst set contains coefcients: a1, a2, …, ad. The second set
gives values of f(1), f(2), …, f(d).
You can assume that: 1 ≤ d ≤ 15, 1 ≤ n ≤ 231 - 1, 1 ≤ m ≤ 46340. All numbers in the input will
ft in signed 32-bit integer.
Input is terminated by line containing three zeroes instead of d, n, m. Two consecutive test cases
are separated by a blank line.
Output
For each test case, print the value of f(n)( mod m) on a separate line. It must be a non-negative integer,
less than m.
Sample Input
1 1 100
21
2 10 100
1 1
1 1
3 2147483647 12345
12345678 0 12345
1 2 3
0 0 0
Sample Output
1
55
423
/*
有一个线性递推关系f(n) = a1*f(n-1) +…… + an*f(n-d),当他是斐波那契数列时d=2,a1=a2=1,计算f(n)对m取余,这是一个适用范围很广的模型,要多看几遍
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = ;
ll sz,mod,f[maxn],a[maxn],ans[maxn];
struct mtx{
ll v[maxn][maxn];
void clear(){
memset(v,,sizeof(v));
}
mtx mul(mtx A,mtx B){
mtx C;
C.clear();
for(int i = ;i <= sz;i++){
for(int j = ;j <= sz;j++){
for(int k = ;k <= sz;k++){
C.v[i][j] = (C.v[i][j] + A.v[i][k]*B.v[k][j]) % mod;
}
}
}
return C;
}
mtx pow(mtx A,int n){
mtx R;
R.clear();
for(int i = ;i <= sz;i++) R.v[i][i] = ;
while(n){
if(n&) R = R.mul(R,A);
n >>= ;
A = A.mul(A,A);
}
return R;
}
void get_tr(mtx A){
for(int i = ;i <= sz;i++){
for(int j = ;j <= sz;j++){
ans[j] = (ans[j] + f[i]*A.v[i][j]) % mod;
}
}
}
};
int main(){
ll d,n,m;
while(cin >> d >> n >> m && d) {
mtx A;
for(int i = ; i <= d; i++) { cin >> a[i]; a[i] %= m; }
for(int i = d; i >= ; i--) { cin >> f[i]; f[i] %= m; }
A.clear();
memset(ans,,sizeof(ans));
for(int i = ; i <= d; i++) A.v[i][] = a[i];
for(int i = ; i <= d; i++) A.v[i-][i] = ; sz = d;
mod = m;
A = A.pow(A,n-d);
A.get_tr(A);
cout << ans[] << endl;
}
return ;
}
uva10870 递推关系Recurrences的更多相关文章
- UVA10870递推关系(矩阵乘法)
题意: 给以个递推f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d.,给你n ...
- UVA10870—Recurrences(简单矩阵快速幂)
题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...
- UVA10870 Recurrences —— 矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...
- UVA10870 Recurrences (矩阵快速幂及构造方法详解)
题意: F(n) = a1 * F(n-1) + a2 * F(n-2)+ ···· + ad * F(n-d). 求给你的n . 很明显这是一道矩阵快速幂的题目. 题解: [Fn-1, Fn-2, ...
- UVa 10870 (矩阵快速幂) Recurrences
给出一个d阶线性递推关系,求f(n) mod m的值. , 求出An-dv0,该向量的最后一个元素就是所求. #include <iostream> #include <cstdio ...
- UVA 10870 - Recurrences(矩阵高速功率)
UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...
- uva 10870 递推关系矩阵快速幂模
Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...
- POJ 1664 放苹果( 递推关系 )
链接:传送门 思路:苹果m个,盘子n个.假设 f ( m , n ) 代表 m 个苹果,n个盘子有 f ( m , n ) 种放法. 根据 n 和 m 的关系可以进一步分析: 特殊的 n = 1 || ...
- uva10870
https://vjudge.net/problem/UVA-10870 裸的矩阵快速幂 注意系数矩阵在前面 因为系数矩阵为d*d 方程矩阵为d * 1 放反了就是d * 1 d * d 不符合矩阵乘 ...
随机推荐
- Linux Nginx(master-slave)、Apache(woker、prefork) Working Mode Research
catalog . Apache工作模式 . Nginx工作模式 1. Apache工作模式 Apache服务器支持三种工作模式(Apache称之为MPM,简写为Multi-Processing Mo ...
- dedecms /install/index.php.bak Installation File Not Deleted && Executed Via Apache Analytic Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 概括梳理一下这个漏洞的成因 . dedecms默认情况下安装完成之后,i ...
- [IOS swift对比oc]
http://www.cocoachina.com/industry/20140605/8686.html WWDC 2014上苹果再次惊世骇俗的推出了新的编程语言Swift 雨燕, 这个消息会前没有 ...
- [JavaEE]Java NIO原理图文分析及代码实现
转http://weixiaolu.iteye.com/blog/1479656 目录: 一.java NIO 和阻塞I/O的区别 1. 阻塞I/O通信模型 2. java NIO ...
- K米测评
第一部分 调研,评测 K米APP使用体验 从小五音不全,所以KTV这类地方我是能不去就不去的,更别说为了点个歌去下个APP,所以K米是我第一个点歌类APP.说说第一次上手的体验吧,APP颜值一般吧,U ...
- Android中如何像 360 一样优雅的杀死后台Service而不启动
http://my.oschina.net/mopidick/blog/277813 目录[-] 一.已知的 kill 后台应用程序的方法 方法: kill -9 pid 二.终极方法,杀死后台ser ...
- ubuntu14.04设置开机亮度
1 查看自己的系统亮度的最大值: cd /sys/class/backlight 笔记本的显卡型号不同->亮度调节文件夹名会不同. 2 我的是intel_backlight cd intel_b ...
- JavaWeb---总结(一)JavaWeb开发入门
一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...
- gcc编译与gdb调试简要步骤
http://blog.chinaunix.net/uid-24103300-id-108248.html 一.Linux程序gcc编译步骤: Gcc编译过程主要的4个阶段: l 预处理阶段,完成宏定 ...
- ASP.NET 递归将分类绑定到 TreeView
CREATE TABLE [dbo].[sysMenuTree]([NoteId] [decimal](18, 0) NOT NULL,[ParentId] [decimal](18, 0) NULL ...