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 ...
随机推荐
- mysql百万数据分页查询速度
百万数据测试 ,; 受影响的行: 时间: .080ms ,; 受影响的行: 时间: .291ms ,; 受影响的行: 时间: .557ms ,; 受影响的行: 时间: .821ms ,; 受影响的行: ...
- Java 8 (6) Stream 流 - 并行数据处理与性能
在Java 7之前,并行处理集合非常麻烦.首先你要明确的把包含数据的数据结构分成若干子部分,然后你要把每个子部分分配一个独立的线程.然后,你需要在恰当的时候对他们进行同步来避免竞争,等待所有线程完成. ...
- NHibernate3.2学习笔记
一.开发环境 数据库:SQLServer2008 编译器:VS2010 .Net版本:.Net Framework 4.0 二.涉及第三方程序集 NHibernate.dll:版本3.2 Iesi.C ...
- opencv识别验证码的教程和资料
简书教程:https://www.jianshu.com/p/41127bf90ca9 博客园教程(较详细):https://www.cnblogs.com/qqandfqr/p/7866650.ht ...
- HDU_1114_piggy-bank
Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- Python游戏开发:pygame游戏开发常用数据结构
一.数组与列表 数组可以理解为简化的列表.像我们之前使用的pygame.sprite.Group这样的精灵组,也是一个列表.列表的元素是可变的,它具有添加.删除.搜索.排序等多种方法. 1.一维列表 ...
- 扩增子分析QIIME2-2数据导入Importing data
# 激活工作环境 source activate qiime2-2017.8 # 建立工作目录 mkdir -p qiime2-importing-tutorial cd qiime2-importi ...
- axios方法封装
axios方法封装 一般情况下,我们会用到的方法有:GET,POST,PUT,PATCH,封装方法如下: 五.封装后的方法的使用 1.在main.js文件里引用之前写好的文件,我的命名为htt ...
- MS SQL Server查询 本日、本周、本月、本季度、本年起始时间
参数声明 declare @beginTime datetime, --查询开始时间 @endTime datetime, --查询结束时间 @queryTimeType tinyint; --查询时 ...
- 06Microsoft SQL Server 完整性约束
Microsoft SQL Server 完整性约束 标识 IDENTITY自动编号 CREATE TABLE table_name( id ,), NAME ) not null, sex ) de ...