HDU6050(矩阵快速幂)
来源:互联网 发布:抱枕材料 知乎 编辑:程序博客网 时间:2024/06/03 20:01
求:
公式:
其中
证明参看大佬博客
直接矩阵快速幂求解
code:
#include<iostream>#include"string.h"#include<stdio.h>using namespace std;const int bc=2;const int mod = 1000000007;struct matrix{ long long x[bc][bc];};matrix mutimatrix(matrix a,matrix b){ matrix temp; memset(temp.x,0,sizeof(temp.x)); for(int i=0;i<bc;i++) //答案的行 { for(int j=0;j<bc;j++) //答案的列 { for(int k=0;k<bc;k++) { temp.x[i][j]+=a.x[i][k]*b.x[k][j]; temp.x[i][j]%=mod; temp.x[i][j]+=mod; temp.x[i][j]%=mod; } } } return temp;}matrix sub(matrix a,matrix b){ matrix temp; memset(temp.x,0,sizeof(temp.x)); for(int i=0;i<bc;i++) //答案的行 { for(int j=0;j<bc;j++) //答案的列 { temp.x[i][j]=a.x[i][j]-b.x[i][j]; temp.x[i][j]%=mod; temp.x[i][j]+=mod; temp.x[i][j]%=mod; } } return temp;}matrix powmatrix(matrix a,long long b){ matrix temp; memset(temp.x,0,sizeof(temp.x)); //初始化矩阵为单位阵 for(int i=0;i<bc;i++) temp.x[i][i]=1; while(b) { if(b%2==1) temp=mutimatrix(temp,a); a=mutimatrix(a,a); b=b/2; } return temp;}int main(){ int kase; scanf("%d",&kase); long long n; long long m; while(kase--) { cin>>n>>m; matrix a,b; if(n%(2LL)==0) { b.x[0][0]=1;b.x[0][1]=0; b.x[1][0]=0;b.x[1][1]=1; } else{ b.x[0][0]=-1;b.x[0][1]=2; b.x[1][0]=1;b.x[1][1]=0; } a.x[0][0]=0;a.x[0][1]=2; a.x[1][0]=1;a.x[1][1]=1; matrix temp=powmatrix(a,n); temp=sub(temp,b); matrix res=powmatrix(temp,m-1); long long ans=res.x[0][0]+res.x[1][0]; ans%=mod; ans+=mod; ans%=mod; cout<<ans<<endl; } return 0;}
阅读全文
0 0
- HDU6050(矩阵快速幂)
- HDU6050 Funny Function[矩阵快速幂]
- hdu6050 推公式矩阵快速幂
- Funny Function————(hdu6050)矩阵快速幂
- hdu6050 Funny Function【打表+找规律+矩阵快速幂】
- 2017多校训练第二场 hdu6050 Funny Function(数学+快速幂+逆元)
- hdu6050
- 2017杭电多校联赛第二场-Funny Function (hdu6050)快速幂解数学方程
- poj3070(矩阵快速幂,矩阵乘法)
- UVA10655矩阵快速幂(构造矩阵)
- 快速幂,矩阵快速幂(模板)
- 快速幂(矩阵快速幂)
- 快速矩阵快速幂
- 矩阵相关(研究总结,矩阵,矩阵快速幂)
- 矩阵快速幂(模板)
- hdu2604(矩阵快速幂)
- hdu4968(矩阵快速幂)
- hdu5015(矩阵快速幂)
- 基于机器学习对于短波通讯抗干扰可行性分析
- LeetCode -- Range Sum Query
- Android增加一个System Service
- hbase分布式系统搭建
- 通过dubbo-demo源码学习maven多项目聚合
- HDU6050(矩阵快速幂)
- C++ Map常见用法说明
- EditBox的使用
- APK 安装过程 及 原理 详解
- arduino上跑freertos 运行多任务
- 继承和多态
- HDU Today
- NTLM
- modf的使用