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的更多相关文章

  1. UVA10870递推关系(矩阵乘法)

    题意:       给以个递推f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d.,给你n ...

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

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

  3. UVA10870 Recurrences —— 矩阵快速幂

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

  4. UVA10870 Recurrences (矩阵快速幂及构造方法详解)

    题意: F(n) =  a1 * F(n-1) + a2 * F(n-2)+ ···· + ad * F(n-d). 求给你的n . 很明显这是一道矩阵快速幂的题目. 题解: [Fn-1, Fn-2, ...

  5. UVa 10870 (矩阵快速幂) Recurrences

    给出一个d阶线性递推关系,求f(n) mod m的值. , 求出An-dv0,该向量的最后一个元素就是所求. #include <iostream> #include <cstdio ...

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

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

  7. uva 10870 递推关系矩阵快速幂模

    Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...

  8. POJ 1664 放苹果( 递推关系 )

    链接:传送门 思路:苹果m个,盘子n个.假设 f ( m , n ) 代表 m 个苹果,n个盘子有 f ( m , n ) 种放法. 根据 n 和 m 的关系可以进一步分析: 特殊的 n = 1 || ...

  9. uva10870

    https://vjudge.net/problem/UVA-10870 裸的矩阵快速幂 注意系数矩阵在前面 因为系数矩阵为d*d 方程矩阵为d * 1 放反了就是d * 1 d * d 不符合矩阵乘 ...

随机推荐

  1. siege详解

    简介 siege是一款HTTP/FTP负载测试和基准压测工具   Download http://download.joedog.org/siege/siege-latest.tar.gz   安装 ...

  2. nginx ssl证书安装配置

    原理图: - 客户端生成一个随机数 random-client,传到服务器端(Say Hello) - 服务器端生成一个随机数 random-server,和着公钥,一起回馈给客户端(I got it ...

  3. JUnit报错:java.lang.ClassNotFoundException:

    只要把Java--------compiler-------building-------Buil path problems ------- incomplete build path 和 Circ ...

  4. python面相对象进阶

    1. 类的成员 python 类的成员有三种:字段.方法.属性 字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段 属于对象,只有对象创建 ...

  5. GNU CMAKE 笔记

    最近在调试OJ, 忙了4天多, 最后的问题是judge模块不能正常工作. judge 模块就是两个C++源文件, 它的工作是 从数据库获取用户提交的源码 测评 将测评结果写到数据库 测评部分是与数据库 ...

  6. JS+JavaBean判断管理员增删改的操作权限

    目标:用户分管理员和普通用户2种,都可以登陆,但是管理员才可以执行增删改的权限,普通用户可以看,但是执行的时候提示权限不足 帖代码片段:我只会这一种,在JSP页面判断 省略得权限数值方法: <% ...

  7. C语言函数指针的用法

    函数指针是一种在C.C++.D语言.其他类 C 语言和Fortran 2003中的指针.函数指针可以像一般函数一样,用于调用函数.传递参数.在如 C 这样的语言中,通过提供一个简单的选取.执行函数的方 ...

  8. POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)

    题目链接 题意:两种操作:一是指定区间的数全都加上一个数,二是统计指定区间的和 参考斌神的代码 #include <iostream> #include <cstring> # ...

  9. 我所了解的cgi

    http://www.cnblogs.com/liuzhang/p/3929198.html 当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在 ...

  10. ./yy.sh -d bash 执行脚本时所加的参数

    -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fil ...