Jzzhu and Sequences CodeForces

来源:互联网 发布:linux系统的卸载命令 编辑:程序博客网 时间:2024/05/18 03:05

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

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

Input

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

Output

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

Example
Input
2 33
Output
1
Input
0 -12
Output
1000000006
Note

In the first sample, f2 = f1 + f3,3 = 2 + f3,f3 = 1.

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


思路:构建矩阵


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int mod=1e9+7;ll x,y;ll f[2][2];ll tmp[2][2];ll res[2][2],b[2][2];void multi(ll f[][2],ll b[][2],ll n){    memset(tmp,0,sizeof(tmp));    for(int i=0;i<2;i++)        for(int j=0;j<2;j++)        for(int k=0;k<2;k++)        tmp[i][j]=((tmp[i][j]+f[i][k]*b[k][j])%mod+mod)%mod;    for(int i=0;i<2;i++)        for(int j=0;j<2;j++)        f[i][j]=tmp[i][j];}void Pow(ll n){    memset(res,0,sizeof(res));    for(int i=0;i<2;i++)        res[i][i]=1;    while(n)    {        if(n&1)            multi(res,f,n);        multi(f,f,n);        n>>=1;    }}int main(){    ll n;    while(~scanf("%lld%lld",&x,&y))    {        f[0][0]=f[1][0]=1;        f[0][1]=-1;f[1][1]=0;        scanf("%lld",&n);        if(n==1)        {            if(x<0)            {                printf("%lld\n",x+mod);            }            else                printf("%lld\n",x);            continue;        }        Pow(n-2);        ll ans;        ans=res[0][0]*y%mod+res[0][1]*x%mod;        printf("%lld\n",(ans%mod+mod)%mod);    }    return 0;}


阅读全文
0 0