CodeForces 450B
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).
/**
题意:f[i] = f[i-1] + f[i+1]
做法:矩阵 如题要求建一个二维矩阵,
0 1
-1 0
然后求解
**/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define SIZE 2
#define MOD 1000000007
#define clr( a, b ) memset( a, b, sizeof(a) )
typedef long long LL; struct Mat
{
LL mat[ SIZE ][ SIZE ];
int n;
Mat( int _n )
{
n = _n;
clr( mat, );
}
void init()
{
for( int i = ; i < n; ++i )
for( int j = ; j < n; ++j )
mat[i][j] = ( i == j );
}
Mat operator * ( const Mat &b ) const
{
Mat c( b.n );
for( int k = ; k < n; ++k )
for( int i = ; i < n; ++i ) if( mat[i][k] )
for( int j = ; j < n; ++j )
c.mat[i][j] = ( c.mat[i][j] + mat[i][k] * b.mat[k][j] ) % MOD;
return c;
}
}; Mat fast_mod( Mat a, int b )
{
Mat res( a.n );
res.init();
while( b )
{
if( b & ) res = res * a;
a = a * a;
b >>= ;
}
return res;
} int main()
{
LL x, y, n, res;
scanf( "%lld %lld %lld", &x, &y, &n );
if( n == )
{
printf( "%lld\n", ( x % MOD + MOD ) % MOD );
}
else if( n == )
{
printf( "%lld\n", ( y % MOD + MOD ) % MOD );
}
else
{
n -= ;
Mat C( );
C.mat[][] = ;
C.mat[][] = ;
C.mat[][] = -;
C.mat[][] = ;
C = fast_mod( C, n );
res = ( ( x * C.mat[][] + y * C.mat[][] )% MOD + MOD ) % MOD;
printf( "%lld\n", res );
}
return ;
}
CodeForces 450B的更多相关文章
- CodeForces 450B 矩阵
A - Jzzhu and Sequences Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- CodeForces 450B (矩阵快速幂模板题+负数取模)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- codeforces 450B. Jzzhu and Sequences 解题报告
题目链接:http://codeforces.com/problemset/problem/450/B 题目意思:给出 f1 和 f2 的值,以及n,根据公式:fi = fi-1 + fi+1,求出f ...
- CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD
题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...
- CodeForces 450B Jzzhu and Sequences
矩阵快速幂. 首先得到公式 然后构造矩阵,用矩阵加速 取模函数需要自己写一下,是数论中的取模. #include<cstdio> #include<cstring> #incl ...
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
- Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
随机推荐
- Python3简单入门
在Mac和Linux上运行Python时,请打开终端,然后运行python3 Mac OSX 正确地同时安装Python 2.7 和Python3: http://www.jianshu.com/p ...
- Promise用法总结
1. Promise的状态 Promise对象有三个状态: 1. 进行中(pending) 2. 成功(resolved) 3. 失败(rejected) 2. 生成一个Promise对象 ...
- Linux之socket套接字编程20160704
介绍套接字之前,我们先看一下传输层的协议TCP与UDP: TCP协议与UDP协议的区别 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UD ...
- 解决无法安装cnpm,cnpm卡顿问题
# 注册模块镜像 npm set registry https://registry.npm.taobao.org # node-gyp 编译依赖的 node 源码镜像 npm set disturl ...
- 2017-7-18-每日博客-关于Linux下的history的常用命令.doc
History history命令可以用来显示曾执行过的命令.执行过的命令默认存储在HOME目录中的.bash_history文件中,可以通过查看该文件来获取执行命令的历史记录.需要注意的是.bash ...
- POJ3177:Redundant Paths(并查集+桥)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19316 Accepted: 8003 ...
- jdbcType和javaType
MyBatis 通过包含的jdbcType类型 BIT FLOAT CHAR TIMESTAMP OTHER UNDEFINED TINYINT REAL VARCHAR BINARY BLOB NV ...
- http中有关缓存相关的几个字段
转载自:http://blog.csdn.net/lifeibo/article/details/5979572 Expires.Cache-Control.Last-Modified. ETag是R ...
- Tomcat 映射虚拟目录和程序热部署
虚拟目录的设置 方法一:在${tomcat安装目录}/conf/Catalina/localhost目录下创建一个xml文件,任意文件名都可以,但是此文件名是web应用发布后的虚拟目录: 比如创建一个 ...
- 上下文路径request.getContextPath();与${pageContext.request.contextPath}
(1) request.getContextPath();与${pageContext.request.contextPath}都是获取上下文路径: 1. request.getContextPath ...