Problem A
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

 

sl: 比较裸的快速幂。 拿来练练手,主要是代码。

 1 // by caonima
 2 // hehe
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 using namespace std;
 8 const int MAX= 1e5+;
 9 typedef long long LL;
 typedef vector<LL> vec;
 typedef vector<vec> mat;
 LL a[MAX],f[MAX];
 LL d,n,m;
 
 mat mul(mat &A,mat &B) {
     mat C(A.size(),vec(B[].size(),));
     for(int i=;i<A.size();i++) {
         for(int k=;k<B.size();k++) {
             for(int j=;j<B[].size();j++) {
                 C[i][j]=(C[i][j]+A[i][k]*B[k][j])%m;
             }
         }
     }
     return C;
 }
 
 mat pow(mat A, LL n) {
     mat B(A.size(),vec(A.size(),));
     for(int i=;i<A.size();i++) B[i][i]=;
     while(n>) {
         if(n&) B=mul(A,B);
         A=mul(A,A);
         n>>=;
     }
     return B;
 }
 
 int main() {
     while(scanf("%lld %lld %lld",&d,&n,&m)==&&(d||n||m)) {
         for(int i=;i<d;i++) {
             scanf("%lld",&a[i]);
         }
         for(int i=;i<d;i++) {
             scanf("%lld",&f[i]);
         }
         mat A(d,vec(d,));
         vec B(d,);
         for(int i=;i<d;i++) A[][i]=a[i];
         for(int i=;i<d;i++) A[i][i-]=;
         for(int i=;i<d;i++) B[i]=f[i];
         A=pow(A,n-d);
         LL ans=;
         for(int i=;i<d;i++) {
             ans=(ans+(LL)A[][i]*B[d-i-])%m;
         }
         printf("%lld\n",ans);
     }
     return ;
 }

UVA - 10870 UVA - 10870的更多相关文章

  1. UVA - 12001 UVa Panel Discussion

    Description  UVa Panel Discussion  The UVa online judge team is arranging a panel discussion for the ...

  2. Uva 12124 Uva Live 3971 - Assemble 二分, 判断器, g++不用map.size() 难度:0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  3. UVA 725 UVA 10976 简单枚举

    UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件 ...

  4. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  5. Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)

     Count the Trees  Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...

  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 1354 Mobile Computing | GOJ 1320 不加修饰的天平问题 (例题 7-7)

    传送门1(UVa): https://uva.onlinejudge.org/external/13/1354.pdf 传送门2(GOJ): http://acm.gdufe.edu.cn/Probl ...

  8. 专题复习--背包问题+例题(HDU 2602 、POJ 2063、 POJ 1787、 UVA 674 、UVA 147)

    *注 虽然没什么人看我的博客但我还是要认认真真写给自己看 背包问题应用场景给定 n 种物品和一个背包.物品 i 的重量是 w i ,其价值为 v i ,背包的容量为C.应该如何选择装入背包中的物品,使 ...

  9. UVa 10870 - Recurrences

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. 在数据库中生成txt文件到网络驱动器中(计算机直接创建的网络驱动器在sql server中没有被找到)

    环境:sql server 2008 一.创建网络驱动器映射 语法:exec master..xp_cmdshell 'net use Z: \\ip地址\网络路径 密码 /user:用户名' 例如: ...

  2. 10.11NOIP模拟题(2)

    /* string水过 */ #include<bits/stdc++.h> #define N 1001 using namespace std; int n,x,y,m,pre; st ...

  3. 为什么要用Go语言做后端

    FMZ数字货币量化平台 www.fmz.com, 后端使用Go语言,这里是创始人Zero谈论使用Go语言所带了的便利.原帖地址:https://www.zhihu.com/question/27172 ...

  4. (前缀和 内存分配)51NOD 1081 子段求和

    给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和.   例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 ...

  5. LINQ数据库技术

    LINQ(Language Integrated Qyery),中文名字是语言集成查询.它提供一个统一的编程概念和语法,编程人员不需要关心将要访问的是关系数据库还是XML数据,或是远程的对象,它都采用 ...

  6. 关于java中replace的用法

    今天突然看到Java中的replace有两种方法,一种是直接替换,另一种是可以进行匹配替换的方式: public String replace(CharSequence target, CharSeq ...

  7. mysql之replace into与 insert into duplicat key for update

    mysql实际应用中,我们在插入数据的时候,经常遇到主键冲突的情况,这是因为库中已经存在相同主键的数据,这时,我们只能更新数据:在判断是更新数据还是插入数据,我们还需要在此之前做一些必要的判断:在my ...

  8. FCC 基础JavaScript 练习2

    1. 引号不是字符串中唯一的可以被转义字符.下面是常见的转义序列列表: \'  单引号 \" 双引号 \\ 反斜杠符 \n 换行符 \r 回车符 \t 制表符 \b 退格符 \f  换页符 ...

  9. python+opencv+Face++实现人脸识别比对

    2018-03-2010:16:55 代码仓库--GitHub--https://github.com/az666/python_opencv_face- 依旧是先来图片 下面这张是我进行识别的效果( ...

  10. C语言中结构体大小计算

    1.普通结构体 struct student { char sex; char a; char b; int age; char name[100]; }; 该结构体大小为108 解答:1.先算str ...