矩阵的连乘

这是一篇关于计算序列连乘性质的问题,给定x和y,要求计算fn模1000000007。输入包含x、y的值以及序列长度n,输出fn的模值。示例解释了如何根据序列性质计算特定项的值。
摘要由CSDN通过智能技术生成


B - 矩阵快速幂
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Jzzhu has invented a kind of sequences, they meet the following property:

You are given x and y, please calculate fn modulo 1000000007(109 + 7).

Input

The first line contains two integers x and y(|x|, |y| ≤ 109). The second line contains a single integer n(1 ≤ n ≤ 2·109).

Output

Output a single integer representing fn modulo 1000000007(109 + 7).

Sample Input

Input
2 3
3
Output
1
Input
0 -1
2
Output
1000000006

Hint

In the first sample, f2 = f1 + f33 = 2 + f3f3 = 1.

In the second sample, f2 =  - 1 - 1 modulo (109 + 7) equals (109 + 6).


可以找规律~~~~

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int LL;

const LL mod=1000000007LL;
LL a,b,n;

int main()
{
  cin>>a>>b>>n;
  n--;
  n=n%6;
  LL ans=0;
  switch(n)
  {
    case(0): 
    {
      ans=(a+mod)%mod;
      break;
    }
    case(1):
    {
      ans=(b+mod)%mod;
      break;
    }
    case(2):
    {
      ans=(b-a+2*mod)%mod;
      break;
    }
    case(3):
    {
      ans=(mod-a)%mod;
      break;
    }
    case(4):
    {
      ans=(mod-b)%mod;
      break;
    }
    case(5):
    {
      ans=(a-b+2*mod)%mod;
      break;
    }
  }
  cout<<ans<<endl;
  return 0;
}

也可以利用矩阵的连乘~~~

#include<cstdio>


int main()
{
    int a,b,n,i,k,rep;
    while(scanf("%d%d%d",&a,&b,&n)!=EOF){
        int matrix[2][2]={1,0,-1,1};
        int ans[2][2]={1,0,0,1};
        k=n-2;
        if(a<0)a+=1000000007;
        if(b<0)b+=1000000007;
        for(;k;k>>=1){
            if(k&1){
                ans[0][0]=ans[0][0]*matrix[0][0]+ans[0][1]*matrix[1][0];//matrix's plus
                ans[0][1]=ans[0][0]*matrix[0][1]+ans[0][1]*matrix[1][1];
                ans[1][0]=ans[1][0]*matrix[0][0]+ans[1][1]*matrix[1][0];
                ans[1][1]=ans[1][0]*matrix[0][1]+ans[1][1]*matrix[1][1];
                //ans=Mplus(ans,matrix);
            }
            matrix[0][0]=matrix[0][0]*matrix[0][0]+matrix[0][1]*matrix[1][0];//so do it
            matrix[0][1]=matrix[0][0]*matrix[0][1]+matrix[0][1]*matrix[1][1];
            matrix[1][0]=matrix[1][0]*matrix[0][0]+matrix[1][1]*matrix[1][0];
            matrix[1][1]=matrix[1][0]*matrix[0][1]+matrix[1][1]*matrix[1][1];
           // matrix=Mplus(matrix,matrix);
        }
        rep=ans[1][0]*a+ans[1][1]*b;
        for(;rep<=0;rep+=1000000007);
        rep%=1000000007;
        printf("%d\n",rep);

    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值