题目大意:已知递推公式和边缘值,求某项的最后m(0<m<5)位数字。

题目分析:矩阵二分幂的模板题。

代码如下:

 # include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
struct matrix
{
int r,c,m[][];
matrix(int _r,int _c):r(_r),c(_c){}
};
int a,b,n,m;
int mod[]={,,,};
matrix multiply(matrix a,matrix b)
{
matrix c(a.r,b.c);
for(int i=;i<=c.r;++i){
for(int j=;j<=c.c;++j){
c.m[i][j]=;
for(int k=;k<=a.c;++k){
c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
c.m[i][j]%=mod[m-];
}
}
}
return c;
}
matrix matrix_pow(matrix a,int k)
{
if(k==){
for(int i=;i<=a.r;++i)
for(int j=;j<=a.c;++j)
a.m[i][j]=(i==j)?:;
return a;
}
if(k==)
return a;
matrix res=matrix_pow(a,k/);
res=multiply(res,res);
if(k&)
res=multiply(res,a);
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&a,&b,&n,&m);
if(n==){
printf("%d\n",a%mod[m-]);
continue;
}
matrix mat(,);
mat.m[][]=mat.m[][]=mat.m[][]=,mat.m[][]=;
mat=matrix_pow(mat,n-);
matrix ans(,);
ans.m[][]=b,ans.m[][]=a;
ans=multiply(mat,ans);
printf("%d\n",ans.m[][]);
}
return ;
}

UVA-10689 Yet another Number Sequence (矩阵二分幂模板)的更多相关文章

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

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

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

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

  3. POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】

    典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...

  4. UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)

    题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...

  5. UVA 10689 Yet another Number Sequence

    简单矩阵快速幂. if(m==1) MOD=10; if(m==2) MOD=100; if(m==3) MOD=1000; if(m==4) MOD=10000; 剩下的就是矩阵快速幂求斐波那契数列 ...

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

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

  7. 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 ...

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

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

  9. 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 ...

随机推荐

  1. Struts2快速后台验证 使用

    为了和前台基于JavaScript的开源验证框架RapidValidation使用统一的验证规则, 最大限度的减少重复的后台验证代码, 使用方式简便, 扩展方便. https://blog.csdn. ...

  2. 来了解一下Redis的分布式锁

    分布式锁本质上要实现的目标就是在 Redis 里面占一个“茅坑”,当别的进程也要来占 时,发现已经有人蹲在那里了,就只好放弃或者稍后再试. 占坑一般是使用 setnx(set if not exist ...

  3. python模块-random随机数模块

    导入随机数模块import random 1.random.random() 生成[0,1)之间的随机小数 2.random.randint(a,b) 生成[a,b]之间的随机整数 3.random. ...

  4. phonegap 开发案例

    PhoneGap-Android-HTML5-WebSocket 不使用任何框架,教你制作网页滑动切换效果 http://www.csdn.net/article/2012-04-17/2804644 ...

  5. 03: 自定义异步非阻塞tornado框架

    目录:Tornado其他篇 01: tornado基础篇 02: tornado进阶篇 03: 自定义异步非阻塞tornado框架 04: 打开tornado源码剖析处理过程 目录: 1.1 源码 1 ...

  6. Starting MySQL...The server quit without updating PID file [失败]local/mysql/data/localhost.localdomain.pid报错

    在添加命令自动补全的时候mysql启动失败 这是原配 # For advice on how to change settings please see # http://dev.mysql.com/ ...

  7. Python3基础 list 查看filter()返回的对象

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. ElasticSearch 5.4 安装

        1. 前期准备  环境准备 IP地址 操作系统 内存 192.168.1.10 centos 7 16 192.168.1.11 centos 7 16 192.168.1.12 centos ...

  9. .NET反射的优化

    写在前面 1)本篇博客并非原创,而是我针对.NET反射相关知识的总结.本篇内容来源汇总于3篇博客.在后面的介绍中会在开头给出对应的链接,方便读者自行学习.2)本篇博客主要针对表达式树代码进行详细讲解. ...

  10. BZOJ 1068 【SCOI2007】 压缩

    题目链接:压缩 区间动归水题.稍微有一点细节. 令\(f_{l,r}\)表示区间\([l,r]\)最短压缩长度,默认\(l\)位置之前有个\(M\).然后就枚举一下放不放\(R\),\(M\)放哪个位 ...