Recurrences Input: standard input Output: standard output

Consider recurrent functions of the following form:

f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), for n > d. a1, a2, ..., ad - arbitrary constants.

A famous example is the Fibonacci sequence, defined 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 coefficients: 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 file contains several test cases. Each test case begins with three integers: dnm, followed by two sets of d non-negative integers. The first set contains coefficients: 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 fit 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                              Output for Sample Input

1 1 100
2
1
 
2 10 100
1 1
1 1
 
3 2147483647 12345
12345678 0 12345

1 2 3

0 0 0

1
55
423

 


题目大意:f(n)=a1*f(n-1)+a2*f(n-2)+.....+ad*f(n-d)

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; #define Max 20
typedef long long LL; struct Matrix
{
LL a[Max][Max];
int n;
}; Matrix Matrix_mult_mod(Matrix A,Matrix B,int m)
{
int i,j,k;
Matrix C;
C.n=A.n;
memset(C.a,,sizeof(C.a));
for(i=;i<=A.n;i++)
{
for(j=;j<=A.n;j++)
{
for(k=;k<=A.n;k++)
{
C.a[i][j]=(C.a[i][j]+A.a[i][k]*B.a[k][j])%m;
}
}
}
return C;
}

Matrix Matrix_pow_mod(Matrix A,int n,int m)
{
Matrix t;
int i,j;
t.n=A.n;
memset(t.a,,sizeof(t.a));
for(i=;i<=A.n;i++) t.a[i][i]=;
for(i=;i<=A.n;i++)
for(j=;j<=A.n;j++)
A.a[i][j]%=m;
while(n)
{
if(n&) t=Matrix_mult_mod(t,A,m);
n>>=;
A=Matrix_mult_mod(A,A,m);
}
return t;
} void deal(int d,int n,int m)
{
int i,j;
LL dd[Max],dd1[Max];
Matrix A;
A.n=d;
memset(A.a,,sizeof(A.a));
for(i=,j=;j<=d;i++,j++) A.a[i][j]=;
for(j=d,i=;i<=d;i++,j--) scanf("%ll",&A.a[d][j]);
for(i=;i<=d;i++) scanf("%ll",dd+i);
A=Matrix_pow_mod(A,n-d,m);
for(i=;i<=d;i++)
{
dd1[i]=;
for(j=;j<=d;j++)
dd1[i]=(dd1[i]+A.a[i][j]*dd[j])%m;
}
printf("%ll\n",dd1[d]);
}

int main()
{
int d,n,m;
while(scanf("%d %d %d",&d,&n,&m),d+n+m)
deal(d,n,m);
return ;
}

uva 10870 递推关系矩阵快速幂模的更多相关文章

  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. hdu 4602 递推关系矩阵快速幂模

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:输出第 n 年向上小三角形的个数 % 10^9 + 7 思路: 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - ...

  4. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  5. 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...

  6. UVA - 10870 Recurrences 【矩阵快速幂】

    题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...

  7. 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) ...

  8. uva 10518 - How Many Calls?(矩阵快速幂)

    题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...

  9. HDU 2842 Chinese Rings( 递推关系式 + 矩阵快速幂 )

    链接:传送门 题意:解 N 连环最少步数 % 200907 思路:对于 N 连环来说,解 N 连环首先得先解 N-2 连环然后接着解第 N 个环,然后再将前面 N-2 个环放到棍子上,然后 N 连环问 ...

随机推荐

  1. (六)mybatis之构建SqlSessionFactory

    构建SqlSessionFactory 每个mybatis应用都是以SqlSessionFactory的实例为中心的.SqlSessionFactory的实例可以通过SqlSessionFactory ...

  2. 微软OneDrive使用体验

    OneDrive是微软推出的一款软件,提供类似百度网盘的功能,能够在线存储照片和文档, 号称从任意电脑.Mac 电脑或手机都可访问. 一起来看看吧,第一次用之前需要进行简单配置. 因为是一个同步盘,需 ...

  3. Servlet The Request

    The Request HTTP Protocol Parameters 所有的HTTP Protocol Parameters都会放在一个Map中, 可以通过getParameterMap得到. 对 ...

  4. Luogu P4463 [国家集训队] calc

    WJMZBMR的题果然放在几年后看来仍然挺神,提出了一种独特的优化DP的方式 首先我们想一个暴力DP,先定下所有数的顺序(比如强制它递增),然后最后乘上\(n!\)种排列方式就是答案了 那么我们容易想 ...

  5. C++数据文件存储与加载(利用opencv)

    首先请先确认已经安装好了opencv3及以上版本. #include <opencv2/opencv.hpp>#include <iostream>#include <s ...

  6. JS中的事件、事件冒泡和事件捕获、事件委托

    https://www.cnblogs.com/diver-blogs/p/5649270.html https://www.cnblogs.com/Chen-XiaoJun/p/6210987.ht ...

  7. python_112_网络编程 Socket编程

    实例1:客户端发小写英文,服务器端返回给客户端大写英文(仅支持一次接受发送) 服务器端: #服务器端(先于客户端运行) import socket server=socket.socket() ser ...

  8. Android Studio 中安装 apk 被拆分成多个 slice,如何禁止?

    Android Studio 3.0.1 中,Run 'app' 时,生成的 apk 被分割成多个 slice: $ adb install-multiple -r D:\...\app\build\ ...

  9. PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)

    http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...

  10. database---many to many relationships(多对多关系型数据库)

    Many to many Relationships A many-to-many relationship occurs when multiple records in a table are a ...