Yet another Number Sequence

Let’s define another number sequence, given by the following function:
f(0) = a
f(1) = b
f(n) = f(n − 1) + f(n − 2), n > 1
When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values of a and
b, you can get many different sequences. Given the values of a, b, you have to find the last m digits of
f(n).
Input
The first line gives the number of test cases, which is less than 10001. Each test case consists of a
single line containing the integers a b n m. The values of a and b range in [0,100], value of n ranges in
[0,1000000000] and value of m ranges in [1,4].
Output
For each test case, print the last m digits of f(n). However, you should NOT print any leading zero.
Sample Input
4
0 1 11 3
0 1 42 4
0 1 22 4
0 1 21 4
Sample Output
89
4296
7711
946

题意:

给你 f[0],f[1] 分别为A,B求F[n] % (10^m)

题解:

n有点大,矩阵快速幂

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll; const int N = + ;
const int mod = 1e9 + ;
const int M[] = {, , , , };
struct Matrix {
ll mat[][];
}U,F,L;
ll MOD;
Matrix multi (Matrix a, Matrix b) {
Matrix ans;
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
ans.mat[i][j] = ;
for(int k = ; k < ; k++)
ans.mat[i][j] += a.mat[i][k] * b.mat[k][j];
ans.mat[i][j] %= MOD;
}
}
return ans;
}
ll a,b,m;
Matrix powss(ll n) {
Matrix ans = L,p = U;
while(n) {
if(n&) ans = multi(p,ans);
n >>= ;
p = multi(p,p);
}
return ans;
}
int main() { int T;
scanf("%d",&T);
while(T--) {
ll n;
scanf("%lld%lld%lld%lld",&a,&b,&n,&m);
U = {,,,};
L = {b,,a,};
MOD = M[m];
Matrix ans = powss(n);
printf("%lld\n",ans.mat[][]);
}
return ;
}

UVA - 10689 Yet another Number Sequence 矩阵快速幂的更多相关文章

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

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

  2. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  3. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  4. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  5. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  6. Yet another Number Sequence 矩阵快速幂

    Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...

  7. SDUT1607:Number Sequence(矩阵快速幂)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...

  8. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

  9. CodeForces 392C Yet Another Number Sequence 矩阵快速幂

    题意: \(F_n\)为斐波那契数列,\(F_1=1,F_2=2\). 给定一个\(k\),定义数列\(A_i=F_i \cdot i^k\). 求\(A_1+A_2+ \cdots + A_n\). ...

随机推荐

  1. 利用js在文本框末尾获得焦点

    function moveEnd(obj) { obj.focus(); var len = obj.value.length; if (document.selection) { var sel = ...

  2. 01背包问题(回溯法)python实现

    接上一篇,相同的01背包问题,上一篇採用动态规划的方法,如今用回溯法解决. 回溯法採用深度优先策略搜索问题的解.不多说.代码例如以下: bestV=0 curW=0 curV=0 bestx=None ...

  3. 开源ext2read代码走读之-在windows下怎样推断有几个硬盘设备?

    int get_ndisks() {     HANDLE hDevice;               // handle to the drive to be examined     int n ...

  4. android WebViewClient和WebChromeClient

    一.Android之WebViewClient与WebChromeClient的区别 ANDROID应用开发的时候可能会用到WEBVIEW这个组件,使用过程中可能会接触到WEBVIEWCLIENT与W ...

  5. php面向对象之构造函数和析构函数

    php面向对象之构造函数和析构函数 简介 php面向对象支持两种形式的构造函数和析构函数,一种是和类同名的构造函数(php5.5之前),一类是魔术方法(php5.5之后).与类名相同的构造函数优先级比 ...

  6. 2.linux系统命令详解

    1 shell shell:命令解释器,根据输入的命令执行相应命令. 1.1 shell家族 察看当前系统下有哪些shell: cat /etc/shells 察看当前系统正在使用的shell ech ...

  7. oracle故障处理之删除大表空间hang住

    背景 数据库分区表数据越来越大,需要对过期话的数据进行迁移,以及大的分区表需要进行数据的清理和删除,达到释放磁盘空间的目的. 问题说明 环境:linux 6.X 数据库:oracle 11.2.0.4 ...

  8. 使用右键打开Visual Code

    Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shell\Visual Code]@="Edit with Visual ...

  9. Makefile经典教程(转)

    转自:http://blog.csdn.net/ruglcc/article/details/7814546/ makefile很重要 什么是makefile?或许很多Winodws的程序员都不知道这 ...

  10. 依赖注入Unity框架

    依赖注入和控制反转是对同一件事情的不同描述,从某个方面讲,就是它们描述的角度不同.依赖注入是从应用程序的角度在描述,可以把依赖注入描述完整点:应用程序依赖容器创建并注入它所需要的外部资源:而控制反转是 ...