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).

Sample test(s)
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).

#include <iostream>
#include <cstring>
using namespace std;
/*矩阵快速幂*/
#define mod 1000000007
#define ll long long int
#define sec 2
struct mat
{
ll arr[sec][sec];
};
mat mul(mat a, mat b)
{
mat res;
memset(res.arr, , sizeof(res.arr));
for (int i = ; i < sec; i++)
for (int j = ; j < sec; j++)
for (int k = ; k < sec; k++)
{
res.arr[i][j] = ((res.arr[i][j] + a.arr[i][k] * b.arr[k][j]) % mod + mod) % mod;
}
return res;
}
mat expo(mat ori, int n)
{
mat res;
memset(res.arr, , sizeof(res.arr));
for (int i = ; i < sec; i++)
res.arr[i][i] = ;
while (n)
{
if (n & )
res = mul(res, ori);
ori = mul(ori, ori);
n >>= ;
}
return res;
}
int main()
{
int x, y, n;
while (cin >> x >> y >> n)
{
x = (x + mod) % mod;
y = (y + mod) % mod;
if (n == )
cout << x << endl;
else
if (n == )
cout << y << endl;
else
{
mat a;
a.arr[][] = a.arr[][] = ;
a.arr[][] = -;
a.arr[][] = ;
a = expo(a, n - );
cout << ((a.arr[][] * y) % mod + (a.arr[][] * x) % mod) % mod << endl;
}
}
return ;
}

Codeforces450 B. Jzzhu and Sequences的更多相关文章

  1. Codeforces450 B. Jzzhu and Sequences (找规律)

    题目链接:https://vjudge.net/problem/CodeForces-450B Jzzhu has invented a kind of sequences, they meet th ...

  2. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  3. Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂

    题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...

  5. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  6. CF450B Jzzhu and Sequences(矩阵加速)

    CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...

  7. 数学 找规律 Jzzhu and Sequences

    A - Jzzhu and Sequences   Jzzhu has invented a kind of sequences, they meet the following property: ...

  8. Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...

  9. Codeforces Round #257 (Div. 2) B Jzzhu and Sequences

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

随机推荐

  1. python代码覆盖率coverage简介与用法

    如果衡量单元测试对相应代码的测试重量,覆盖率是一个必要非充分条件,因此统计代码的覆盖率,检视单测是否充分,就尤为的重要.这里针对python-unittest的单测的覆盖率coverage进行使用说明 ...

  2. (五)Python面向对象编程

    根据廖雪峰老师的python教程写一些学习总结! 面向对象编程 面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元, ...

  3. LoadRunner12学习之路(1-5)

    本次LoadRunner12学习用户指南,学习周期预计3天,每天学习1-2单元内容! 2017.12.17 一.使用HPE Web Tours示例应用程序 本教程使用 HPE Web Tours(一个 ...

  4. HDU 5996 博弈

    http://acm.hdu.edu.cn/showproblem.php?pid=5996 博弈论待补. 这题变化了一下,因为注意到奇数层的东西(层数从1开始),对手可以模仿地动,那就相当于没动. ...

  5. poj3735Training little cats

    链接 构造矩阵 快速幂求解 构造矩阵a[i]为每个cati所拥有的花生总数 这里多加一维用来求和,具体是怎么求得可以看下面的一组例子 假设有3个cat a[] = {1,0,0,0} 构造单位矩阵来保 ...

  6. 解决ASP.NET Core通过docker-compose up启动应用无法配置https的解决办法

    错误重现一下: 新建了一个ASP.NET Core应用,在VS2017下添加Docker支持,选择Linux环境 然后再给这个web应用再右键添加容器业务流程协调程序支持,然后解决方案就多了一个doc ...

  7. AJPFX关于File类复习

    file是一个路径,分为相对路径(eclipse)和绝对路径:1.构造方法有:File(String pathname ),File(String parent ,String child),File ...

  8. Java BigDecimal类的使用和注意事项

    1.对于金额相关运算,若是精度较高,基本上用BigDecimal进行运算,精度要求低的话用Long.Double即可 2.web后台接受金额用String接受,展示到前端一般也转成 String 3. ...

  9. vue学习之遇见的问题

    1.本地图片加载不出来 错误原因:图片放置位置不对: 解决方法:需要将图片放在static文件夹里

  10. PHP(二)常用函数

    数学函数 数组函数 字符串函数