CF450B——水矩阵

来源:互联网 发布:linux 网络编程本地ip 编辑:程序博客网 时间:2024/05/21 10:59

传送门:http://codeforces.com/problemset/problem/450/B

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 33
output
1
input
0 -12
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).


题意:题意很简单,就是给递推式写答案。把题中公式移项就可以了,构造就不说了,主要在小细节。这里是用了矩阵快速幂的优化,比较果

注意:对负数取余要先加mod再mod

代码:

#include<iostream>#include<sstream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<ctime>#include<list>#include<vector>#include<set>#include<map>#include<stack>#include<queue>#pragma GCC optimize("02")#define cl(a,b) memset(a,b,sizeof(a))#define in freopen("F://1.txt","r",stdin)#define out freopen("F://2.txt","w",stdout)using namespace std;typedef unsigned long long llu;typedef long long ll;const ll inf=(ll)1<<60;const int maxn=1e5+7;const int MAXN = 2;long long mod = 1000000007;struct Matrix{    long long mat[MAXN][MAXN];    void Zero(){        cl(mat, 0);        //memset(mat, 0, sizeof(mat));    }    void Unit(){        //memset(mat, 0, sizeof(mat));        cl(mat, 0);        for (int i = 0; i < MAXN; i++)            mat[i][i] = 1;    }    void output(){        for (int i = 0; i < MAXN; i++){            for (int j = 0; j < MAXN; j++){                printf("%d ", mat[i][j]);            }            printf("\n");        }    }};Matrix operator*(Matrix &a, Matrix &b){    Matrix tmp;    tmp.Zero();///初始化    for (int k = 0; k < MAXN; k++){        for (int i = 0; i < MAXN; i++){            if (!a.mat[i][k])                continue;            for (int j = 0; j < MAXN; j++){                tmp.mat[i][j] += a.mat[i][k] * b.mat[k][j] % mod;                if ( tmp.mat[i][j] >= mod)                    tmp.mat[i][j] -= mod;            }        }    }    return tmp;}Matrix operator ^(Matrix a, int k){    Matrix tmp;///单位矩阵    tmp.Unit();    if(k<=1)        return a;    for (; k; k >>= 1){        if (k & 1)            tmp = tmp * a;        a = a * a;    }    return tmp;}int main(){    int i,j;    ll x, y, n;    Matrix t;    t.mat[0][0] = 1; t.mat[0][1] = -1;    t.mat[1][0] = 1; t.mat[1][1] = 0;    while(~scanf("%lld %lld %lld",&x, &y, &n)){        if(n==1){            printf("%lld\n",(x%mod+mod)%mod);            continue;        }        else if(n==2){            printf("%lld\n",(y%mod+mod)%mod);            continue;        }        else{            Matrix temp = t ^ (n - 2);            ll ans = ((temp.mat[0][0]*y%mod + temp.mat[0][1]*x%mod)%mod+mod)%mod;            printf("%lld\n",ans);        }    }    return 0;}


原创粉丝点击