矩阵基础1006 HDU 4549
2016-07-27 12:39
344 查看
题意:
F[0] = a
F[1] = b
F
= F[n-1] * F[n-2] ( n > 1 )
求F(N)
思路:
没有直接构造出来矩阵,就把乘法转化成了幂的加法
然后就比较容易构造矩阵了
F(n)=F(n-1)+F(n-2)+1
注意一点是取模的时候因为是指数取模,所以MOD 1e9+6
1e9+6=phi(1e9+7) 这里是欧拉函数的知识
F[0] = a
F[1] = b
F
= F[n-1] * F[n-2] ( n > 1 )
求F(N)
思路:
没有直接构造出来矩阵,就把乘法转化成了幂的加法
然后就比较容易构造矩阵了
F(n)=F(n-1)+F(n-2)+1
注意一点是取模的时候因为是指数取模,所以MOD 1e9+6
1e9+6=phi(1e9+7) 这里是欧拉函数的知识
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<queue> #include<stack> #include<string> #include<vector> #include<map> #include<set> using namespace std; #define lowbit(x) (x&(-x)) typedef long long LL; const int maxn = 100005; const int inf=(1<<28)-1; #define Matrix_Size 5 const LL MOD = 1e9+6; int Size; struct Matrix { LL mat[Matrix_Size][Matrix_Size]; void clear() { memset(mat,0,sizeof(mat)); } void output() { for(int i = 0;i < Size;i++) { for(int j = 0;j < Size;j++) printf("%lld ",mat[i][j]); printf("\n"); } } Matrix operator *(const Matrix &b)const { Matrix ret; for(int i = 0;i < Size;i++) for(int j = 0;j < Size;j++) { ret.mat[i][j] = 0; for(int k = 0;k < Size;k++) { long long tmp = (long long)mat[i][k]*b.mat[k][j]%MOD; ret.mat[i][j] = (ret.mat[i][j]+tmp); if(ret.mat[i][j]>=MOD) ret.mat[i][j] -= MOD; if(ret.mat[i][j]<0)//注意是否需要MOD ret.mat[i][j] += MOD; } } return ret; } }; Matrix pow_M(Matrix a,long long n) { Matrix ret; ret.clear(); for(int i = 0;i < Size;i++) ret.mat[i][i] = 1; Matrix tmp = a; while(n) { if(n&1)ret = ret*tmp; tmp = tmp*tmp; n>>=1; } return ret; } LL quickMod(LL a,LL b,LL mod) { LL res=1,tmp=a; while(b) { if(b&1) res=(res*tmp)%mod; tmp=(tmp*tmp)%mod; b/=2; } return res; } int main() { LL a,b,n; while(~scanf("%lld%lld%lld",&a,&b,&n)) { Size=4; Matrix A,B; A.clear(),B.clear(); A.mat[0][0]=A.mat[0][2]=1; A.mat[1][1]=A.mat[1][3]=1; A.mat[2][0]=A.mat[3][1]=1; B.mat[1][0]=B.mat[2][0]=1; if(n==0) { printf("%lld\n",a%(MOD+1)); continue; } if(n==1) { printf("%lld\n",b%(MOD+1)); continue; } A=pow_M(A,n-1); A=A*B; //A.output(); LL Ans=(quickMod(a,A.mat[0][0],MOD+1)*quickMod(b,A.mat[1][0],MOD+1))%(MOD+1); printf("%lld\n",Ans); } return 0; }
相关文章推荐
- 从头学android_alertDialog的使用
- CSU 1723: 想打架吗?算我一个!所有人,都过来!
- 射频全网通笔记(附全球频段划分及主要运营商对应表)
- 从零开始的Android新项目9 - 前端用后台接口设计
- mysql 优化
- 助词复习—1
- CRM----管理软件分析( ERP/CRM/OA)
- hdu2196 Computer 【树形dp】
- Spring(二) 依赖注入
- 顺序表应用6:有序顺序表查询
- 腾讯课堂学习资源链接地址
- TextView深度解析
- Manacher 字符串回文查询算法说解
- hdu5754(简单的博弈)
- 从零开始的Android新项目8 - Data Binding高级篇
- 从零开始的Android新项目7 - Data Binding入门篇
- HDU 5762(暴力)
- unity批量拖拽到属性排序问题小技巧
- 从零开始的Android新项目6 - Repository层(下) Realm、缓存、异常处理
- Java ArrayList转数组泛型方法错误ArrayStoreException处理办法