UVA-10689 Yet another Number Sequence (矩阵二分幂模板)

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

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

代码如下:

 1 # include<iostream>
 2 # include<cstdio>
 3 # include<cstring>
 4 # include<algorithm>
 5 using namespace std;
 6 struct matrix
 7 {
 8     int r,c,m[3][3];
 9     matrix(int _r,int _c):r(_r),c(_c){}
10 };
11 int a,b,n,m;
12 int mod[4]={10,100,1000,10000};
13 matrix multiply(matrix a,matrix b)
14 {
15     matrix c(a.r,b.c);
16     for(int i=1;i<=c.r;++i){
17         for(int j=1;j<=c.c;++j){
18             c.m[i][j]=0;
19             for(int k=1;k<=a.c;++k){
20                 c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
21                 c.m[i][j]%=mod[m-1];
22             }
23         }
24     }
25     return c;
26 }
27 matrix matrix_pow(matrix a,int k)
28 {
29     if(k==0){
30         for(int i=1;i<=a.r;++i)
31             for(int j=1;j<=a.c;++j)
32                 a.m[i][j]=(i==j)?1:0;
33         return a;
34     }
35     if(k==1)
36         return a;
37     matrix res=matrix_pow(a,k/2);
38     res=multiply(res,res);
39     if(k&1)
40         res=multiply(res,a);
41     return res;
42 }
43 int main()
44 {
45     int T;
46     scanf("%d",&T);
47     while(T--)
48     {
49         scanf("%d%d%d%d",&a,&b,&n,&m);
50         if(n==0){
51             printf("%d\n",a%mod[m-1]);
52             continue;
53         }
54         matrix mat(2,2);
55         mat.m[1][1]=mat.m[1][2]=mat.m[2][1]=1,mat.m[2][2]=0;
56         mat=matrix_pow(mat,n-1);
57         matrix ans(2,1);
58         ans.m[1][1]=b,ans.m[2][1]=a;
59         ans=multiply(mat,ans);
60         printf("%d\n",ans.m[1][1]);
61     }
62     return 0;
63 }
时间: 2024-10-12 13:16:05

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

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

                  Yet another Number Sequence Let’s de?ne another number sequence, given by the following function:f(0) = af(1) = bf(n) = f(n − 1) + f(n − 2), n > 1When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values

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

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 4; int Mod; int msize; struct Mat { int mat[N][N]; }; Mat operator *(Mat a, Mat b) { Mat c; mems

Uva10689 Yet another Number Sequence ( 矩阵快速幂 )

Uva 10689Yet another Number Sequence(  矩阵快速幂  ) 题意: 就是矩阵快速幂,没什么好说的. 分析: 其实还是斐波那契数列.只是最后对应的矩阵不是(1,1)是(a,b)了 MOD = 1; for( int i = 0; i < m; ++i ) MOD *= 10; 代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace s

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; 剩下的就是矩阵快速幂求斐波那契数列第n项取模 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; long long

HDU 1005 Number Sequence 矩阵快速幂

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 236241    Accepted Submission(s): 60070 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

SDUT1607:Number Sequence(矩阵快速幂)

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). 输入

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

矩阵快速幂模板篇

转载请注明出处:http://blog.csdn.net/u012860063 或许你们看不太懂,纯属自用: 第一种: Description 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 seq