UVA-10689 Yet another Number Sequence (矩阵二分幂模板)
题目大意:已知递推公式和边缘值,求某项的最后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 (矩阵二分幂模板)的更多相关文章
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
		Yet another Number Sequence Let’s define another number sequence, given by the foll ... 
- UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水
		#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ... 
- 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) ... 
- 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 ... 
- 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; 剩下的就是矩阵快速幂求斐波那契数列 ... 
- Yet Another Number Sequence——[矩阵快速幂]
		Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ... 
- 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 ... 
- HDU - 1005 Number Sequence 矩阵快速幂
		HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ... 
- 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 ... 
随机推荐
- Js基础知识2-对象、对象属性全解
			Object对象 Object对象包含如下属性和方法,也就意味着一切对象(函数也是对象)都包含如下方法. 每种方法和属性在不同的对象中有不同的作用,并不是每种对象都有使用每个方法的必要. 下面是Obj ... 
- bootstrap table数据分页查询展示
			index.php <html> <head> <link rel="stylesheet" href="./css/bootstrap.m ... 
- ORA-01507: database not mounted
			今天启动数据库时报错了! SQL> startup mount ORACLE instance started. Total System Global Area 608174080 byte ... 
- PHP发送HTTP请求的6种方法
			方法1: 用 file_get_contents 以get方式获取内容: <?php$url = 'https://wenda.shukaiming.com/';echo file_get_co ... 
- P3435 [POI2006]OKR-Periods of Words
			P3435 [POI2006]OKR-Periods of Words 题解传送门 kmp 注意:由于题目说只要A满足是2Q的前缀,所以求的不是严格的最大循环子串(20pts) 我们需要求出的是在主串 ... 
- Cortex-M3基础
			(一)寄存器 1 寄存器组 R0-R12: 通用寄存器 ------------------------------------------------------------------- ... 
- 如何写出格式优美的javadoc?
			如果你读过Java源码,那你应该已经见到了源码中优美的javadoc.在eclipse 中鼠标指向任何的公有方法都会显示出详细的描述,例如返回值.作用.异常类型等等. 本文主要来自<Thinki ... 
- 【前端】javascript+jquery实现手风琴式的滚动banner或产品展示图
			实现效果 实现步骤 // 鼠标放入到li中该盒子变宽,其他盒子变窄,鼠标移开大盒子,恢复原样 // 实现步骤 // 1. 给li添加背景 // 2. 绑定onmouseover事件,鼠标放入到li中, ... 
- Python3基础 file read 读取txt文件的前几个字符
			Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ... 
- Linq let Concat
			let: String[] strs = { "A penny saved is a penny earned.", "The early bird catches th ... 
