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. java 实现word文档在线预览

    一.准备工具 1.通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件 下载地址:http://www.openoffice.org/download/in ...

  2. deepin 安装版本管理工具

    在Linux下我们可以使用RapidSVN.RapidSVN是一款轻量级的免费.开源 SVN 客户端,相比tortoise svn它更加小巧而且占系统资源少运行速度快. 一:安装RapidSVN版本控 ...

  3. GIT学习之路最终日 标签管理+总结

    本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 6.1 创建标签 命令git tag (name)用于新建一个标签,默认为HEAD,也可以指定一个commit id: git tag -a ...

  4. 洛谷 P2090 数字对

    发现如果给定两个数(a,b),可以用类似辗转相除法在logn的时间内计算出(反向)变到(1,1)的最小步数. 然而并不知道另一个数是多少? 暴力嘛,枚举一下另一个数,反正1000000的nlogn不虚 ...

  5. 离散化+BFS HDOJ 4444 Walk

    题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...

  6. VMWare虚拟网络编辑

    VMWare虚拟网络编辑记录. 点击"编辑->虚拟网络编辑器" 在网卡列表中选择"VMnet8"查看目前设置,点击右下角的更改设置进行网络修改. 备注:更 ...

  7. ViewPager(2)FragmentStatePagerAdapter示例

    FragmentStatePagerAdapter适用于数量比较大的viewPager,只会存在 前1个fragment 当前fragment 和下1个fragment,其他销毁,适合加载多数据.本例 ...

  8. Android 性能优化(9)网络优化( 5)Optimizing Server-Initiated Network Use

    Optimizing Server-Initiated Network Use This lesson teaches you to Send Server Updates with GCM Netw ...

  9. js中true和false判断

    布尔类型里只有这6参数个返回false,其它都为true Boolean(false) // false Boolean(undefined) // false Boolean(null) // fa ...

  10. Android基础TOP6_3:Gally和ImageSwitcher实现画廊

    结构: Activity: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...