CodeForces 450B 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 + 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值