题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数。

分析:n最大为109,矩阵快速幂求解,复杂度log2(109)。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-9;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1000007;
const double pi = acos(-1.0);
const int MAXN = 400 + 10;
const int MAXT = 1000 + 10;
using namespace std;
int POW[10];
int a, b, n, m;
void init(){
POW[1] = 10;
for(int i = 2; i <= 4; ++i){
POW[i] = POW[i - 1] * 10;
}
}
struct Matrix{
int r, c;
int matrix[2][2];
Matrix(int rr, int cc):r(rr), c(cc){
memset(matrix, 0, sizeof matrix);
}
};
Matrix mul(Matrix a, Matrix b){
Matrix ans(a.r, b.c);
for(int i = 0; i < a.r; ++i){
for(int j = 0; j < b.c; ++j){
int tmp = 0;
for(int k = 0; k < a.c; ++k){
(tmp += a.matrix[i][k] * b.matrix[k][j]) %= POW[m];
}
ans.matrix[i][j] = tmp;
}
}
return ans;
}
Matrix QPOW(Matrix tmp, int k){
Matrix ans(2, 2);
ans.matrix[0][0] = ans.matrix[1][1] = 1;
while(k){
if(k & 1) ans = mul(ans, tmp);
tmp = mul(tmp, tmp);
k >>= 1;
}
return ans;
}
int main(){
int T;
scanf("%d", &T);
init();
while(T--){
scanf("%d%d%d%d", &a, &b, &n, &m);
Matrix tmp1(2, 2), tmp2(2, 1);
tmp1.matrix[0][0] = tmp1.matrix[0][1] = tmp1.matrix[1][0] = 1;
tmp2.matrix[0][0] = b;
tmp2.matrix[1][0] = a;
Matrix ans = mul(QPOW(tmp1, n - 1), tmp2);
printf("%d\n", ans.matrix[0][0]);
}
return 0;
}

  

UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)的更多相关文章

  1. codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质

    E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  2. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  3. poj3070矩阵快速幂求斐波那契数列

      Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13172   Accepted: 9368 Desc ...

  4. UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  5. 51 Nod 1242 矩阵快速幂求斐波那契数列

    #include<bits/stdc++.h> #define mod 1000000009 using namespace std; typedef long long ll; type ...

  6. 矩阵快速幂 求斐波那契第N项

    #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> us ...

  7. 矩阵快速幂--51nod-1242斐波那契数列的第N项

    斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...

  8. codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...

  9. python 快速幂求斐波那契数列

    先占坑 后面再写详细的 import numpy as np def pow(n): a = np.array([[1,0],[0,1]]) b = np.array([[1,1],[1,0]]) n ...

随机推荐

  1. tomcat重载web项目,debug

    Reloading Context with name [/testCookie] is completed 加载上下文名称[ / ]完成testcookie //start九月 05, 2017 9 ...

  2. Day1-E-BZOJ1293

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩 ...

  3. mac brew redis

    1.搜索brew search redis //出现如下 ==> Searching local taps... hiredis redis redis-leveldb redis@2.8 re ...

  4. git 的一些基本命令

    基本命令 1.返回上一级目录:cd ../ 2.进入某一目录:cd git (进入 git 目录) 3. 显示当前路径:pwd 4.显示当前文件目录的文件 : dir 5.新建文件夹:mkdir +文 ...

  5. 《ES6标准入门》(阮一峰)--12.Symbol

    1.概述 ES5 的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突.如果有一种 ...

  6. MyISAM与InnoDB 的区别

    1. 事务:InnoDB支持,MyISAM不支持,在InnoDB中每一条SQL语句都会默认封装成事务自动提交,然而这样会影响速度,因此最好把多条SQL语句放在begin和commit之间组成一个事务: ...

  7. POJ 3268:Silver Cow Party 求单点的来回最短路径

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15989   Accepted: 7303 ...

  8. JVM配合IDEA远程调试

    前言 如果测试环境中运行的jar包项目报错,需要排错时,会不太方便,这个时候就可以通过远程调试的方式,在本地打断点的方式进行调试 正文 一.首先,运行测试环境的jar包需要加以下参数: java -j ...

  9. 关于Orcale 11g 安装过程

    1.前往Oracle官网下载相应安装包 https://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.h ...

  10. 如何创建一个SpringBoot多模块项目

    创建主模块rail-plate-line 1.点击Create New Project  --> 选择Spring Initializr  -- > 选择本地jdk 2.Group为com ...