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. springboot-i18n国际化

    简介 In computing, internationalization and localization are means of adapting computer software to di ...

  2. css3中animation属性animation-timing-function知识点以及其属性值steps()

    在animation中最重要的其实就是时间函数(animation-timing-function)这个属性,他决定了你的动画将以什么样的速度执行,所以最关键的属性值也就是cubic-bezier(n ...

  3. substring substr slice 区别

    1. substring(start,end)  返回指定索引区间的字串,不改变原字符串 start 必需,开始位置的索引,一个非负的整数 end  可选,结束位置的索引(不包括其本身),如果未设置, ...

  4. grafana绘制图表

    安装方法 系统为ubuntu16 1首先添加以下到/etc/apt/sources.list: deb https://packagecloud.io/grafana/stable/debian/ s ...

  5. JS 绘制心形线

    JS 绘制心形线 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> < ...

  6. 5.电影搜索之 自动填充,也叫autocomplete、搜索建议!

    什么叫自动填充,用过百度的应该都知道!当你输入关键词之后,会有一个下拉的候选列表,都是与你输入的内容相关的,这个就是自动填充的搜索建议.一般的搜索引擎或者站内搜索都会有这个功能. 今天分享下这个功能的 ...

  7. (转)automaticallyAdjustsScrollViewInsets(个人认为iOS7中略坑爹的属性)

    转自http://m.blog.csdn.net/blog/humingtao2013/27662093 automaticallyAdjustsScrollViewInsets(个人认为iOS7中略 ...

  8. STM32L0开发——ADC多通道采集,IDE和IAR开发注意事项

    keil开发L0系列是免费的,官方提供许可的.因此建议Keil开发,L011F3由于flash只有8K,因此不建议HAL库,建议使用cubemx+LL(或snippets库).0.起初,可以参考官方库 ...

  9. Maven运行报错:-Dmaven.multiModuleProjectDirectory system propery is not set.

    eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...

  10. VisionPro工业视觉的标定方法

    工业视觉常用的几种标定方式. 计算像素比 有些时候我们需要的检测数据并不需要特别准确,并且手边没有其它标定工具,可以使用这种方法大概算一算每个像素对应多大距离. 找一个知道距离的物体,测出它的像素距离 ...