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. 疫情控制 2012年NOIP全国联赛提高组(二分答案+贪心)

    P1084 疫情控制 题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控 ...

  2. redis之简单动态字符串(SDS)

    O(N):时间复杂度 N的值越大 时间复杂度随N的平方增大 O(1):就是说N很大的时候,复杂度基本不增长了.基本就是常量了. 在Redis数据库里 包含字符串值的键值对 在底层都是由SDS实现的. ...

  3. Java中的锁机制,你真的了解吗?

    学到锁说明你已经学过多线程了,只有在多线程并发的情况下才会涉及到锁,相信大家用的最多的要数synchronized了,因为这个也是最简单的,直接加在方法上就可以使一个方法同步.那么除了synchron ...

  4. 学习http协议的三次握手和四次挥手 ~~笔记

    http协议是基于tcp协议的  所以应该说是tcp协议的三次握手和四次挥手 SYN:请求建立连接,并在其序列号的字段进行序列号的初始值设定.建立连接,设置为1 FIN:用来释放一个连接.FIN=1表 ...

  5. 如何使用 Idea 远程调试 Java 代码

    起因 这几天,我做的项目中需要使用第三方的 API,在第三方的 API 回调时,出现各种错误,需要远程调试.之前做远程调试的时候,我只会在代码中输出日志,记录下来做分析处理,但这样做既麻烦又费时,往往 ...

  6. 390 Elimination Game 淘汰游戏

    详见:https://leetcode.com/problems/elimination-game/description/ C++: 方法一: class Solution { public: in ...

  7. 372 Super Pow 超级次方

    你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出.示例 1:a = 2b = [3]结果: 8示例 2:a = 2b = [1,0]结果: 102 ...

  8. cloudera-server启动File not found : /usr/sbin/cmf-server解决办法(图文详解)

    解决方法 见 cloudera-agent启动File not found : /usr/sbin/cmf-agent解决办法(图文详解) 欢迎大家,加入我的微信公众号:大数据躺过的坑        ...

  9. C++ const学习

    概念 const就是为了直接表达“不变化的值”这一概念.也就是说该值只可读,不可直接写. 由于不可以修改,所以const常量在声明的时候必须初始化 const int a; //error exter ...

  10. Selenium学习第二天,了解Selenium工作模式与学习Selenium需要具备的知识与工具。

    Selenium学习网站: 1.http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2014/0408/207237.html——好像是对API的 ...