Codeforces450 B. Jzzhu and Sequences
1 second
256 megabytes
standard input
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).
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 a single integer representing fn modulo 1000000007 (109 + 7).
2 3
3
1
0 -1
2
1000000006
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的更多相关文章
- Codeforces450 B. Jzzhu and Sequences (找规律)
题目链接:https://vjudge.net/problem/CodeForces-450B Jzzhu has invented a kind of sequences, they meet th ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- 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 ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...
- CF450B Jzzhu and Sequences(矩阵加速)
CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...
- 数学 找规律 Jzzhu and Sequences
A - Jzzhu and Sequences Jzzhu has invented a kind of sequences, they meet the following property: ...
- 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 ...
- 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 ...
随机推荐
- 解决Bad owner or permissions on .ssh/config 的问题
在使用gei fetch 或者 sftp的时候,出现 Bad owner or permissions on .ssh/config的问题的解决办法 修改.ssh/config的权限: sudo ch ...
- JavaScript中this的使用方法总结
JavaScript中this的使用方法总结 在JavaScript中,this的使用分为四种场景,具体请参考阮一峰老师关于this的讲解 第一种情况是纯函数使用 var x =1 ; functio ...
- (DP ST表 线段树)51NOD 1174 区间中最大的数
给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数为7. ...
- hihoOffer收割练习20题目2
题目2 : SCI表示法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 每一个正整数 N 都能表示成若干个连续正整数的和,例如10可以表示成1+2+3+4,15可以表示 ...
- 洛谷 P3804 【模板】后缀自动机
来一份模板 #include<cstdio> #include<algorithm> #include<cstring> #include<queue> ...
- 背包DP HDOJ 5410 CRB and His Birthday
题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == ...
- Java多线程学习---------超详细总结(java 多线程 同步 数据传递 )
目录(?)[-] 一扩展javalangThread类 二实现javalangRunnable接口 三Thread和Runnable的区别 四线程状态转换 五线程调度 六常用函数说明 使用方式 为什么 ...
- orcl 11g 创建表空间
Oracle11g创建表空间语句 在plsql工具中执行以下语句,可建立Oracle表空间. /*分为四步 *//*第1步:创建临时表空间 */create temporary tablespa ...
- 搞定redis面试--Redis的过期策略?手写一个LRU?
1 面试题 Redis的过期策略都有哪些?内存淘汰机制都有哪些?手写一下LRU代码实现? 2 考点分析 1)我往redis里写的数据怎么没了? 我们生产环境的redis怎么经常会丢掉一些数据?写进去了 ...
- 一个小方法解决RGBA不兼容IE8
原网页http://blog.csdn.net/leihope_/article/details/70158902 要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如 ...