uva 10870 递推关系矩阵快速幂模
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: d, n, m, 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 递推关系矩阵快速幂模的更多相关文章
- UVa 10870 Recurrences (矩阵快速幂)
题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...
- hdu 4602 递推关系矩阵快速幂模
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:输出第 n 年向上小三角形的个数 % 10^9 + 7 思路: 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - ...
- HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...
- 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...
- UVA - 10870 Recurrences 【矩阵快速幂】
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...
- 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) ...
- uva 10518 - How Many Calls?(矩阵快速幂)
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...
- HDU 2842 Chinese Rings( 递推关系式 + 矩阵快速幂 )
链接:传送门 题意:解 N 连环最少步数 % 200907 思路:对于 N 连环来说,解 N 连环首先得先解 N-2 连环然后接着解第 N 个环,然后再将前面 N-2 个环放到棍子上,然后 N 连环问 ...
随机推荐
- elastic-job lite 编程实战经验
(继续贴一篇之前写的经验案例) elastic-job lite 编程实战经验 其实这是一次失败的项目,虽然最后还是做出来了,但是付出了很大代价.并且需要较深入的踩坑改造elastic-job,导致代 ...
- 并查集+思维——Destroying Array
一.题目描述(题目链接) 给定一个序列,按指定的顺序逐一删掉,求连续子序列和的最大值.例如序列1 3 2 5,按3 4 1 2的顺序删除,即依次删除第3个.第4个.第1个.第2个,答案为5 4 3 0 ...
- mysql安装(docker)
mkdir /opt/mysql vim /opt/mysql/Dockerfile 5.7 FROM alpine FROM mysql:5.7.26 EXPOSE 3306 8.0 FROM al ...
- 一个小笔记(5):A*算法
A-Star算法是一种静态路网中求解最短路径最有效的直接搜索方法其实百科有 http://baike.baidu.com/link?url=CvmkWQIAmztYgMq3Nk1WyWkDiC0koV ...
- python之常见的坑
li = [1,2,3,4] # [1,3,4] # 索引值是奇数的删除 for i in range(4): if i % 2 == 1: li.pop(i) # 会报错 print(li) 面试题 ...
- python:加密模块
加密:import hashlib # import md5 #python2 中可以直接引入md5,3中没有 #md5 #md5加密是不可逆的,即不能解密. #只要用MD5加密,结果都是一样的,不区 ...
- Fisherfaces 算法的具体实现源码
/* * Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>. * Released to public domain ...
- ios设备屏幕尺寸与分辨率
iOS 设备的屏幕尺寸.分辨率及其屏幕边长比例详细情况是怎样的? 根据屏幕尺寸和分辨率,ios现在数起来有6个版本.一,3GS:二,4s为代表:三,iphone5:四,ipad2为代表:五,ipad4 ...
- Xcode中的约束用法
这篇文章用几个简单的例子来介绍XCode6.1故事板中约束的使用方法. 现在iOS设备屏幕的尺寸也有很多种了,尤其是有了iPhone6 Plus以后,再不关注界面的尺寸适配就有点说不过去了. ...
- 【分块】[HNOI2010]弹飞绵羊&分块大法祭
分块(似乎还有一种动态树(LCT)做法) 第一次学习分块,似乎有点小激动 这是黄学长的分块入门博客「分块」数列分块入门1 – 9 by hzwer 题目描述 某天,Lostmonkey发明了一种超级弹 ...