矩阵基础1002 CodeForces 450B
来源:互联网 发布:mac怎么查看隐藏文件夹 编辑:程序博客网 时间:2024/06/05 02:03
题意:
F[i]=F[i-1]+F[i+1]
求F[n]
思路:
F[i+1]=F[i]-F[i-1]->F[i]=F[i-1]-F[i-2]
然后矩阵快速幂去做
这道题有些特殊性,简单推导一下就会发现
每6次一个循环,打个小表就行了
#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 5const LL MOD = 1e9+7;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("%d ",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;}int main(){ LL x,y,n; scanf("%lld%lld%lld",&x,&y,&n); if(n==1) { printf("%lld\n",(x+MOD)%MOD); return 0; } Size=2; Matrix A,B; A.mat[0][0]=x,A.mat[0][1]=y; A.mat[1][0]=0,A.mat[1][1]=0; B.mat[0][0]=0,B.mat[0][1]=-1; B.mat[1][0]=1,B.mat[1][1]=1; B=pow_M(B,n-2); A=A*B; //B.output(); //printf("%lld\n",((x*B.mat[1][0])%MOD+(y*B.mat[1][1])%MOD+MOD)%MOD); //printf("%lld\n",A.mat[0][1]); printf("%lld\n",(A.mat[0][1]+MOD)%MOD); return 0;}
0 0
- 矩阵基础1002 CodeForces 450B
- codeForces 271B--素数矩阵
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Codeforces 450B-Jzzhu and Sequences (矩阵快速幂)
- codeforces 450-B Jzzhu and Sequences 矩阵快速幂
- Codeforces 450B Jzzhu and Sequences(矩阵快速幂)
- Codeforces 852B 矩阵乘法优化DP
- Codeforces Round #257 (Div. 2) 450B - Jzzhu and Sequences(循环节或矩阵快速幂)
- 【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)
- Codeforces 450B f【n】=f【n-1】-f【n-2】(矩阵快速幂,裸题)
- CodeForces 450B
- CodeForces-450B
- 基础题 CodeForces - 631B Print Check
- CodeForces 52B Right Triangles 矩阵上的计数
- Codeforces #257B Jzzhu and Sequences(矩阵快速幂)
- 矩阵快速幂 CodeForces - 582B Once Again...
- Codeforces Gym 101061 B RGB plants(矩阵快速幂)
- JQuery 控制html元素显示或隐藏
- 计算几何 ( 凸包 )——Wall ( HDU 1248 )
- 利用docker-compose实现elk+redice的但宿主机环境搭建
- 矩阵基础1001 HDU 4990
- Convolutional neural networks(CNN) (一) 入门参考
- 矩阵基础1002 CodeForces 450B
- 【Maven用户手册】Maven生命周期
- OpenGL,z值转深度值计算
- Python图表绘制:matplotlib绘图库入门
- mysql的安装和用户权限
- android studio 怎么引入签名文件
- 矩阵基础1003 HDU 4965
- Java学习之Iterator(迭代器)的一般用法 (多方整理)
- pdo事物回滚