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. 关于server和虚拟主机的差别

    文章都是先由本人个人博客,孙占兴:www.teilim.com,先更新,随后CSDN博客才会更新.掌握第一动态请关注本人主站. 原文链接:http://www.teilim.com/guan-yu-y ...

  2. angularjs1-5,class,

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  3. angularjs1-3,$apply,$watch

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. PHP7添加swoole扩展

    swoole需要php版本在7.0以上. 1.进入php目录中的bin目录下,通过pecl指令进行安装. cd /usr/local/php7/bin [root@localhost bin]# pw ...

  5. 院校-德国:亚琛工业大学(RWTH)

    ylbtech-院校-德国:亚琛工业大学(RWTH) 1.返回顶部 1. 亚琛工业大学(RWTH)成立于1870年,是德国著名理工类大学之一,也是世界顶尖理工类大学之一 ,长久以来被誉为“欧洲的麻省理 ...

  6. 基于QMP实现对qemu虚拟机进行交互

    本文详解QMP,包含qmp.hmp.qemu-guest-agent的介绍.工作原理.配置方法.范例 小慢哥的原创文章,欢迎转载 目录 ▪ QMP介绍 ▪ QMP语法 ▪ 单独使用qemu,启用QMP ...

  7. Flask-Restful

    定义Restful的视图 安装:pip install flask-restful 如果使用Flask-restful,那么定义视图函数的时候,就要继承flask_restful.Resourse类, ...

  8. python模拟登陆知乎

    ---恢复内容开始--- 在完成前面的阶段的任务之后,我们现在已经能够尝试着去模拟登录一些网站了.在这里我们模拟登录一下知乎做一下实验.笔者在这里总共用了三天多的时间,下面给大家分享一下笔者是怎么一步 ...

  9. php 生成不重复的随机字符串

    md5(uniqid(md5(microtime(true)),true))

  10. HDU 2300 Crashing Robots

    Crashing Robots 题意 模拟多个机器人在四个方向的移动,检测crash robot, crash wall, OK这些状态 这是个模拟题需要注意几点: 理解转变方向后移动多少米,和转动方 ...