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 ...
随机推荐
- angularjs 路由参数
.state('classrooms',{ url: '/classrooms/:id' }) .state('classrooms',{ url: '/classrooms/{id}' }) .st ...
- 计算概论(A)/基础编程练习2(8题)/2:计算书费
#include<stdio.h> int main() { // 声明与初始化 ; // k组测试数据的总费用 double s[k]; // 单价表 double price[]= { ...
- 0-5v转0-20ma和0-5v转4-20ma
0-5v转0-20ma 0-5v转4-20ma
- (一)github之基础概念篇
1.github: 一项为开发者提供git仓库的托管服务, 开发者间共享代码的场所.github上公开的软件源代码全都由git进行管理. 2.git: 开发者将源代码存入名为git仓库的资料库中,而g ...
- python 线程 进程 协程 学习
转载自大神博客:http://www.cnblogs.com/aylin/p/5601969.html 仅供学习使用···· python 线程与进程简介 进程与线程的历史 我们都知道计算机是由硬件和 ...
- 03: Django Model数据库操作
目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...
- VC++ 实现修改文件创建、访问、修改时间属性(转载)
转载:http://sunnysab.blog.163.com/blog/static/18037500920134221295425/ struct _FILETIME { //结构体定义 DWOR ...
- lambda表达式学习
Lambda 表达式是一种可用于创建 委托 或 表达式目录树 类型的 匿名函数 . 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式对于编写 ...
- SQL NULL
表 select CHARACTER_MAXIMUM_LENGTH from information_schema.columns where table_name= 'Alliance' selec ...
- 卸载vs2017
卸载enterprise版本 Microsoft.FSharp.SDK.Core卸载失败Package 'Microsoft.FSharp.SDK.Core,version=15.7.20180605 ...