Matrix..0.0
来源:互联网 发布:matlab编程实例 编辑:程序博客网 时间:2024/06/08 17:51
Input
1 2 3
1 2 5
Output
4
7
Hint
1 2 3 4
7 12 19 30
49 80 129 208
Solution
#include <set>#include <map>#include <list>#include <cmath>#include <ctime>#include <deque>#include <queue>#include <stack>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <cassert>#include <cstdlib>#include <cstring>#include <sstream>#include <iostream>#include <algorithm>#define ALL(x) x.begin(),x.end()#define INS(x) inserter(x,x.begin())typedef long long int LLI;const int MOD = 1000000007;const int N = 4;LLI l,r,t,n;struct Matrix { LLI mat[N][N]; Matrix operator*(const Matrix& m)const { Matrix tmp; for(LLI i = 0 ; i < N ; i++) { for(LLI j = 0 ; j < N ; j++) { tmp.mat[i][j] = 0; for(LLI k = 0 ; k < N ; k++) tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k]*m.mat[k][j])%MOD; tmp.mat[i][j] %= MOD; } } return tmp; }};Matrix m;void print(Matrix d){ LLI i,j; for (i=0;i<4;++i) { for (j=0;j<4;++j) printf("%4lld",d.mat[i][j]); printf("\n"); } printf("\n");}LLI Pow() { Matrix ans,tt=m; for (LLI i=0; i<4; ++i) ans.mat[i][0]=l; for (LLI i=0; i<4; ++i) ans.mat[i][1]=r; for (LLI i=0; i<4; ++i) ans.mat[i][2]=l+r; for (LLI i=0; i<4; ++i) ans.mat[i][3]=l+r+r-1; n--; while(n) { if (n%2) tt = tt*m; //print(tt); n /= 2; m = m*m; //print(m); } ans=ans*tt; //print(ans); return ans.mat[0][t]%MOD;}int main() { while(~scanf("%lld%lld",&l,&r)) { for (LLI i=0; i<4; ++i) m.mat[0][i]=0; m.mat[1][0]=0; m.mat[2][0]=m.mat[3][0]=m.mat[1][1]=m.mat[1][2]=m.mat[1][3]=m.mat[3][1]=1; m.mat[2][1] = m.mat[3][2]=2; m.mat[2][2] = 3; m.mat[2][3]=m.mat[3][3]=4; scanf("%lld", &n); t=(n-1)%4; n=(n-1)/4; if (!n) { if (t==3) printf("%lld\n",r+r+l-1); else if (t==2) printf("%lld\n",r+l); else if (t==1) printf("%lld\n",r); else printf("%lld\n",l); } else printf("%lld\n", Pow()); } return 0;}
0 0
- Matrix..0.0
- 《Matrix》
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- mAtrix
- Matrix
- matrix
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- Matrix
- 对java中FileInputStream、BufferInputStream的理解
- 常用框架、库
- Update layout parameters of view
- Python 新建文件夹与复制文件夹内所有内容
- Android屏幕适配方案(下)
- Matrix..0.0
- 杭电ACM2000 自我感悟
- Pycharm配置
- 搭建samba服务器
- PAT(A)-1127. ZigZagging on a Tree (30)(树的重建)
- Ubuntu 安装 MySQL
- 基于BP神经网络的数字识别基础系统(二)
- spark设计思想
- 有关ajax 不走回调函数的问题解析