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 连环问 ...
随机推荐
- 洛谷 P1926 小书童——刷题大军
题目背景 数学是火,点亮物理的灯:物理是灯,照亮化学的路:化学是路,通向生物的坑:生物是坑,埋葬学理的人. 文言是火,点亮历史宫灯:历史是灯,照亮社会之路:社会是路,通向哲学大坑:哲学是坑,埋葬文科生 ...
- JSON数组不用字符串转换的写法
var organization = []; //机构组织 //初始化用户数据列表中用户机构列的数据源 admin.ajax("GetOrganizationInfo", null ...
- JAVASCRIPT闭包以及原型链
方法内部还有个方法,实例化父方法后,再次调用父方法,可以运行父方法内部的子方法,这样的程序就叫做闭包 DEMO如下: //function outerFn() { // var outerVar = ...
- Luogu P5327 [ZJOI2019]语言
ZJOI2019Day2的温暖题,然后考场上只会大常数的\(O(n\log^3 n)\),就懒得写拿了60pts走人 首先我们简化题意,容易发现每个点能到达的点形成了一个联通块,我们只需要统计出这个联 ...
- MYSQL 注射精华
前言鄙人今天心血来潮突然想写篇文章,鄙人从来没写过文章,如果有错误的地方请多多指教.本文需要有基础的SQL语句知识才可以更好的理解.建议想学习的人多去了解一下SQL语句和编程语言,知己知彼才能百战百胜 ...
- ios 注册功能研究学习
通常,移动App的注册功能通常采用手机号码注册或者邮箱帐号注册. 不过在国内这样隐私堪忧的环境下,需要手机号来注册会流失不少用户.即便是新浪微博这样的应用,需要绑定手机号也令我不信任.除非是像淘宝.支 ...
- 洛谷P1001 A+B Problem
这道题…………还是很简单!!! code: #include <iostream> #include <cstdio> using namespace std; int mai ...
- Java语言的特点和特性
1. Java语言的主要特点: 1. 跨平台性 所谓的跨平台性,是指软件可以不受计算机硬件和操作系统的约束而在任意计算机环境下正常运行.这是软件发展的趋势和编程人员追求的目标.之所以这样说,是因为计算 ...
- Django2.x中url路由的path()与re_path()参数解释
在新版本Django2.x中,url的路由表示用path和re_path代替,模块的导入由django1.x版本的from django.conf.urls import url,include变成现 ...
- WINDOWS下使用Mysql 中碰到的问题记录
问题:在cmd中输入net stop mysql反馈“服务名无效” win+R打开运行窗口,输入 services.msc 查看其中mysql的服务名,比如我的是叫做MySQL80 让我们继续回到最开 ...