UVA - 10870 UVA - 10870
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: 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
|
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的更多相关文章
- UVA - 12001 UVa Panel Discussion
Description UVa Panel Discussion The UVa online judge team is arranging a panel discussion for the ...
- 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 ...
- UVA 725 UVA 10976 简单枚举
UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件 ...
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)
Count the Trees Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...
- UVA 10870 - Recurrences(矩阵高速功率)
UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...
- 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 ...
- 专题复习--背包问题+例题(HDU 2602 、POJ 2063、 POJ 1787、 UVA 674 、UVA 147)
*注 虽然没什么人看我的博客但我还是要认认真真写给自己看 背包问题应用场景给定 n 种物品和一个背包.物品 i 的重量是 w i ,其价值为 v i ,背包的容量为C.应该如何选择装入背包中的物品,使 ...
- UVa 10870 - Recurrences
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- 乐搏讲自动化测试- Python自动化测试前景怎么样(4)
Python言语还能够写爬虫,但仅仅只是爬虫的入门罢了.通过Python入门爬虫比较简略易学,不需要在一开始把握太多太根底太底层的常识就能够很快上手,而且很快能够做出成果,十分合适小白一开始想做出点看 ...
- 洛谷 P3437 [POI2006]TET-Tetris 3D
二维线段树区间更新啊 树套树的外层树,如果是线段树的话一般似乎不能打标记?(毕竟标记不好下传) 然而起码对于这题是可以的...对于外层线段树,每个节点放两个内层线段树dat和setv,分别是得到的值和 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- Python Turtle绘图
1. 画布(canvas) 画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置 1.1 设置画布大小 turtle.screensize(canvwidth=None, ca ...
- sql 获取当前季度期间
select year(getdate())*10000+((month(getdate())/3)*3+1)*100 + 1 --季度第一天 select year(getdate())*10000 ...
- malloc()函数的使用
malloc是向系统申请分配指定size个字节的内存空间.返回值类型为void *类型.void *表示未确定的类型指针.C语言中,void *类型可以强制转换为任何其他类型的指针. 语法:void ...
- Android Bitmap转换WebP图片导致损坏的分析及解决方案
背景 作为移动领域所力推的图片格式,WebP图片在商业领域证明了其应有的价值.基于其他格式的横向对比,其在压缩性能表现,及还原度极为优秀,节省大量的带宽开销.基于可观的效益比,团队早前已开始磋商将当前 ...
- 全3D模具设计自动化解決方案
- 世界上最受欢迎的10个Linux发行版
帮助新的Linux用户在越来越多的Linux发行版中选择最合适的操作系统,是创建这个网页的原因.它列出了迄今为止最流行的10个Linux发行版(另外增加的是FreeBSD,到目前为止最为流行的BSD系 ...
- 网站卡测试用 PageSpeed Insights
这个是google测试网页的;https://developers.google.com/speed/pagespeed/insights/ PageSpeed Insights 简介 PageSpe ...