2016春季练习——矩阵快速幂

来源:CF257B

省赛结束,今天继续训练。

做了一道矩阵快速幂,并且相同了具体特征根方程到底是怎么回事了。。。想想高中真是菜啊。

直接上代码吧,群巨会懂的。。。

代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
///上周才看的。。。以为省赛有这个东西的,结果没有。。。笑crying
/********************
就是这个两个数乘以一个
 --         --
|   1    -1   |
|             |
|   1     0   |矩阵。
 --         --
然后用矩阵快速幂就好
********************/
const long long MOD=1e9+7;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> Matrix;
Matrix m(2,VLL(2));
Matrix mul(Matrix a,Matrix b){
    Matrix c(2,VLL(2));
    for(int i=0;i<2;i++)
    {
        for(int k=0;k<2;k++)
        {
            for(int j=0;j<2;j++)
                c[i][j]=(c[i][j]+a[i][k]*b[k][j]);
        }
    }
    return c;
}
Matrix M_qmi(int n){
    Matrix b(2,VLL(2));
    b[0][0]=1;
    b[1][1]=1;//E-juzhen
    while(n>0)
    {
        if(n&1) b=mul(b,m);
        m=mul(m,m);
        n>>=1;
    }
    return b;
}
int main(){
	int x,y;
	int n;
	scanf("%d%d",&x,&y);
	scanf("%d",&n);
	LL sum;
	if(n==1){
        sum=x;
	}
	else if(n==2){
        sum=y;
	}
	else{
        m[0][0]=1;m[0][1]=-1;
        m[1][0]=1;m[1][1]=0;
        Matrix ans=M_qmi(n-2);
        /*cout<<ans[0][0]<<" "<<ans[0][1]<<endl;
        cout<<ans[1][0]<<" "<<ans[1][1]<<endl;*/
        sum=(ans[0][0]*y+ans[0][1]*x)%MOD;
	}
    while(sum<0)sum+=MOD;
    cout<<sum<<endl;
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值