CodeForces 450B Jzzhu and Sequences

来源:互联网 发布:windows loader 2.2.1 编辑:程序博客网 时间:2024/05/23 22:32

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 + f3, 3 = 2 + f3, f3 = 1.

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

题意是给定一个运算规则,可以算出一串数字,求解这串数字中的某一个。

采用两种方法做:

一开始这个我直接找规律做出来了,6个是一个循环节。代码如下:

/*************************************************************************> File Name: Jzzhu_and_Sequences.cpp> Author: ZhangHaoRan> Mail: chilumanxi@gmail.com> Created Time: 2016年03月12日 星期六 10时23分22秒 ************************************************************************/#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<vector>#include<set>#include<map>#include<queue>#include<list>#include<algorithm>using namespace std;long long x;const long long INF = 1000000007;long long y;long long n;long long mod(long long x){    return (x % INF + INF) % INF;}int main(void){    cin >> x >> y;    cin >> n;    bool flag = false;    long long tempb = n % 6;    if(tempb == 1){        cout << mod(x) << endl;    }    else if(tempb == 0){        cout << mod(x - y) << endl;    }     else if(tempb == 2){        cout << mod(y) << endl;;    }    else if(tempb == 3){        cout << mod(y - x) << endl;    }    else if(tempb == 4){        cout << mod((-1) *  x) << endl;    }    else if(tempb == 5){        cout << mod((-1) * y) << endl;    }        return 0;}

后来看晚上说是有矩阵的做法。于是学习了一下。有下面的式子:

f(i) = f(i - 1) + f(i + 1)

可以推出

f(i + 1) = f(i) - f(i - 1) 同时,令i = i - 1有f(i) = f(i - 1) - f(i - 2)

根据上面两个式子,我们就可以得到这样一个矩阵运算。

[f(i - 1), f(i - 2)] * [1, 1] =[f(i) , f(i - 1)]

[-1, 0]

根据此矩阵运算,可以使用矩阵快速幂解决该问题,直接套模板。代码如下:

/*************************************************************************> File Name: Jzzhu_and_Sequences_by_matrix.cpp> Author: ZhangHaoRan> Mail: chilumanxi@gmail.com> Created Time: 2016年03月12日 星期六 12时52分15秒 ************************************************************************/#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<vector>#include<set>#include<map>#include<queue>#include<list>#include<algorithm>using namespace std;struct matrix{    long long Map[2][2];}b, res;long long x, y, n;const long long INF = 1000000007;long long mod(long long x){    return (x % INF + INF) % INF;}matrix mul(matrix x, matrix y){    matrix z;    for(int i = 0; i < 2; i ++){        for(int j = 0; j < 2; j ++){            z.Map[i][j] = 0;            for(int k = 0; k < 2; k ++){                z.Map[i][j] += (x.Map[i][k] * y.Map[k][j]) % INF;                z.Map[i][j] %= INF;            }        }    }    return z;}void poww(long long x){    b.Map[0][0] = 1;    b.Map[0][1] = -1;    b.Map[1][0] = 1;    b.Map[1][1] = 0;    res.Map[0][0] = res.Map[1][1] = 1;    res.Map[0][1] = res.Map[1][0] = 0;    for(; x; x >>= 1){        if(x & 1)            res = mul(res, b);        b = mul(b, b);    }}int main(void){    cin >> x >> y;    cin >> n;    if(n == 1)        cout << mod(x) << endl;    else if(n == 2){        cout << mod(y) << endl;    }    else{        poww(n - 2);        cout << mod(res.Map[0][0] * y + res.Map[0][1] * x) << endl;    }    return 0;}

 

 

查看原文:http://chilumanxi.org/2016/03/12/codeforces-450b-jzzhu-and-sequences/

0 0
原创粉丝点击