九度 oj 题目1081:递推数列
2017-01-15 15:33
351 查看
http://ac.jobdu.com/problem.php?pid=1081
参考了(照抄了)http://blog.csdn.net/zxasqwedc/article/details/8763967
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#define LL long long
static LL a0,a1,p,q,k;
typedef struct matrix{
LL a[2][2];
} Matrix;
Matrix multiply(Matrix m1, Matrix m2){
Matrix ret ;
ret.a[0][0] = ret.a[0][1] = ret.a[1][0] = ret.a[1][1] = 0;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int t = 0; t < 2; ++t) {
ret.a[i][j] += (m1.a[i][t] * m2.a[t][j])%10000;
}
}
}
return ret;
}
Matrix matrixPow(Matrix mat, LL n){
Matrix ret;
ret.a[0][0] = 1,ret.a[1][0] = 0,ret.a[0][1]=0,ret.a[1][1] = 1;
if(n == 0) return ret;
else if( n==1 ) return mat;
else{
if(n&1){
ret = matrixPow(mat,n/2);
ret = multiply(ret, ret);
ret = multiply(mat,ret);
return ret;
}else{
ret = matrixPow(mat,n/2);
ret = multiply(ret,ret);
return ret;
}
}
}
int main(){
while(scanf("%lld %lld %lld %lld %lld",&a0,&a1,&p,&q,&k)!=EOF){
if(k==0) printf("%lld\n" ,a0);
else if(k==1) printf("%lld\n" ,a1);
else{
Matrix m;
m.a[0][0] = 0,m.a[0][1] = 1,m.a[1][0] = q,m.a[1][1] = p;
Matrix ret = matrixPow(m,k-1);
printf("%lld\n",(ret.a[1][0]*a0+ret.a[1][1]*a1)%10000 );
}
}
}
参考了(照抄了)http://blog.csdn.net/zxasqwedc/article/details/8763967
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#define LL long long
static LL a0,a1,p,q,k;
typedef struct matrix{
LL a[2][2];
} Matrix;
Matrix multiply(Matrix m1, Matrix m2){
Matrix ret ;
ret.a[0][0] = ret.a[0][1] = ret.a[1][0] = ret.a[1][1] = 0;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int t = 0; t < 2; ++t) {
ret.a[i][j] += (m1.a[i][t] * m2.a[t][j])%10000;
}
}
}
return ret;
}
Matrix matrixPow(Matrix mat, LL n){
Matrix ret;
ret.a[0][0] = 1,ret.a[1][0] = 0,ret.a[0][1]=0,ret.a[1][1] = 1;
if(n == 0) return ret;
else if( n==1 ) return mat;
else{
if(n&1){
ret = matrixPow(mat,n/2);
ret = multiply(ret, ret);
ret = multiply(mat,ret);
return ret;
}else{
ret = matrixPow(mat,n/2);
ret = multiply(ret,ret);
return ret;
}
}
}
int main(){
while(scanf("%lld %lld %lld %lld %lld",&a0,&a1,&p,&q,&k)!=EOF){
if(k==0) printf("%lld\n" ,a0);
else if(k==1) printf("%lld\n" ,a1);
else{
Matrix m;
m.a[0][0] = 0,m.a[0][1] = 1,m.a[1][0] = q,m.a[1][1] = p;
Matrix ret = matrixPow(m,k-1);
printf("%lld\n",(ret.a[1][0]*a0+ret.a[1][1]*a1)%10000 );
}
}
}
相关文章推荐
- 九度OJ题目1081:递推数列-快速幂
- 九度OJ 题目1081:递推数列
- 九度OJ题目1081:递推数列解题报告
- 九度OJ题目1081:递推数列-快速幂
- 九度OJ 1081 递推数列 -- 矩阵二分乘法
- 九度OJ 1081 递推数列 -- 矩阵二分乘法
- 九度oj1081递推数列的算法
- 九度OJ-题目1165:字符串匹配
- 【剑指Offer面试编程题】题目1516:调整数组顺序使奇数位于偶数前面--九度OJ
- 【剑指Offer面试编程题】题目1507:不用加减乘除做加法--九度OJ
- 九度oj 题目1049:字符串去特定字符
- 【九度OJ】题目1203:IP地址
- 九度oj 题目1077:最大序列和
- 九度OJ 题目1023:EXCEL排序
- 九度OJ 题目1072:有多少不同的面值组合?
- 【九度OJ】题目1442:A sequence of numbers 解题报告
- 九度OJ题目1034:寻找大富翁
- 九度 OJ 题目1017:还是畅通工程
- 九度OJ题目1036:Old Bill
- 九度OJ 题目1044:Pre-Post