题目大意:已知递推公式和边缘值,求某项的最后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. tmocat ssl 证书 生成 过程

    . "%JAVA_HOME%\bin\keytool" -genkey -alias tomcat -keyalg RSA -keystore D:\Youxun\keystore ...

  2. bzoj4591 / P4345 [SHOI2015]超能粒子炮·改

    P4345 [SHOI2015]超能粒子炮·改 题意:求$\sum_{i=1}^{k}C(n,i)\%(P=2333)$ 肯定要先拆开,不然怎么做呢(大雾) 把$C(n,i)$用$lucas$分解一下 ...

  3. VM三种网络连接方式

    bridge:这种方式最简单,直接将虚拟网卡桥接到一个物理网卡上面,和Linux下一个网卡 绑定两个不同地址类似,实际上是将网卡设置为混杂模式,从而达到侦听多个IP的能力. 在此种模式下,虚拟机内部的 ...

  4. troubleshooting-Container 'PHYSICAL' memory limit

    原因分析 CDH 集群环境没有对 Container分配足够的运行环境(内存) 解决办法 需要修改的配置文件,将具体的配置项修改匹配集群环境资源.如下: 配置文件 配置设置 解释 计算值(参考) ya ...

  5. github客户端上传代码

    在window下安装github客户端上传代码 第一步:创建Github新账户 第二步:新建仓库 第三步:安装Github shell程序,地址:http://windows.github.com/ ...

  6. bzoj 3343: 教主的魔法

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 924  Solved: 402[Submit][Status][Discuss] Descriptio ...

  7. linux的dns被劫持

    环境:ubuntu16.04 解说:ubuntu使用dnsmasq获取要解析的网站ip,dnsmasq通过域名服务器获取网站ip,并将ip缓存起来,那么就可以减少对外网域名服务器的访问,从而可以使系统 ...

  8. LightOJ 1296 Again Stone Game(sg函数)题解

    题意:每次必须拿且只能拿不超过一半的石头,不能拿为败 思路:显然算出每个的sg函数,但是范围1e9显然不能直接打表.所以先打表找规律,发现偶数一直是自己的一半,奇数好像没规律.偶数x的sg函数值是x/ ...

  9. LA 4080 战争和物流(最短路树)

    https://vjudge.net/problem/UVALive-4080 题意:给出一个n个结点m条边的无向图,每条边上有一个正权.令c等于每对结点的最短路长度之和.不连通的两点的最短路长度视为 ...

  10. MyBatis各种类型的入参使用方式

    https://blog.csdn.net/u011983531/article/details/53561219 mybatis中if判断传入字符串或者Long参数不为空 https://blog. ...