数学专项matrix:UVa 10655
来源:互联网 发布:建筑三维设计软件 振华 编辑:程序博客网 时间:2024/05/10 03:20
递推式显然f[n]=p*f[n-1]-q*f[n-2],直接快速幂即可,只不过这题输入有坑。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;typedef LL matrix[3][3];LL p,q;int n;void mat_mul(matrix A,matrix B,matrix res){ matrix C; memset(C,0,sizeof(C)); for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) C[i][j]+=A[i][k]*B[k][j]; memcpy(res,C,sizeof(C));}void mat_pow(matrix A,int n,matrix res){ matrix a,r; memset(r,0,sizeof(r)); memcpy(a,A,sizeof(a)); for(int i=0;i<2;i++) r[i][i]=1; while(n) { if(n&1) mat_mul(r,a,r); n>>=1; mat_mul(a,a,a); } memcpy(res,r,sizeof(r));}int main(){ while(scanf("%lld%lld%d",&p,&q,&n)==3) { if(n==0) { puts("2"); continue; } matrix A,B; A[0][0]=0,A[0][1]=1,A[1][0]=-q,A[1][1]=p; mat_pow(A,n-1,B); LL ans=B[0][0]*p+B[0][1]*(p*p-2*q); printf("%lld\n",ans); } return 0;}
- 数学专项matrix:UVa 10655
- 数学专项matrix:UVa 11149
- 数学专项matrix:UVa 10689
- 数学专项matrix:UVa 11551
- 数学专项matrix:LA 2561
- 数学专项counting:UVa 11038
- 数学专项counting:UVa 10883
- 数学专项counting:UVa 10079
- 数学专项counting:UVa 10081
- 数学专项game_theory:UVa 12293
- 数学专项game_theory:UVa 11892
- 数学专项game_theory:UVa 11927
- 数学专项number_theory:UVa 10515
- 数学专项number_theory:UVa 10622
- 数学专项number_theory:UVa 10127
- 数学专项number_theory:UVa 10090
- 数学专项number_theory:UVa 10539
- 数学专项number_theory:UVa 11105
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
- poj 3278 搜索
- uva 10001 Garden of Eden (DFS)
- 借助PL/SQL将服务器上的oracle10数据库转移到另一台服务器上的方法
- 智能电视资料整理(未完成)
- 数学专项matrix:UVa 10655
- oracle linux5.1 下边安装oracle10g 详解
- 博客开张了
- (转)Microsoft Help Library 管理器安装-更改库位置-错误等总结
- 题目1439:Least Common Multiple
- IE证书查看工具
- uva 140 Bandwidth (下一个序列+判断)
- java回调函数
- 梯度下降算法的python实现