Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences

B. Jzzhu and Sequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Examples
input
2 3
3
output
1
input
0 -1
2
output
1000000006
Note

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

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


比较简单的矩阵快速幂题目;

递推矩阵为|1 -1|

  |1 0 |

#include<bits/stdc++.h>
using namespace std;
struct Mat
{
    long long mat[2][2];
};
Mat operator *(Mat a,Mat b)
{
    Mat c;
    memset(c.mat,0,sizeof(c.mat));
    for(int k=0;k<2;++k)
    {
        for(int i=0;i<2;++i)
        {
            for(int j=0;j<2;++j)
            {
                c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                c.mat[i][j]%=1000000007;
            }
        }
    }
    return c;
}
Mat operator ^(Mat a,int k)
{
    Mat c;
    for(int i=0;i<2;++i)
        for(int j=0;j<2;++j)
            c.mat[i][j]=(i==j);
    for(;k;k>>=1)
    {
        if(k&1) c=c*a;
        a=a*a;
    }
    return c;
}
int main()
{
    int x,y;
    int n;
    scanf("%d %d",&x,&y);
    scanf("%d",&n);
    int ans=0;
    if(n==1)
    {
        ans=x%1000000007;
    }
    else if(n==2)
    {
        ans=y%1000000007;
    }
    else
    {
        Mat a;
        a.mat[0][0]=1;
        a.mat[0][1]=-1;
        a.mat[1][0]=1;
        a.mat[1][1]=0;
        Mat temp;
        temp=a^(n-2);
        ans=(temp.mat[0][0]*y+temp.mat[0][1]*x)%1000000007;
    }
    if(ans<0) ans+=1000000007;
    printf("%d\n",ans);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值