CF450B——水矩阵

传送门: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 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).


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

注意:对负数取余要先加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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值