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 ...
随机推荐
- BZOJ1507 [NOI2003]Editor 【splay】
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MB Submit: 4129 Solved: 1660 [Submit][St ...
- CodeForces.71A Way Too Long Words (水模拟)
CodeForces71A. Way Too Long Words (水模拟) 题意分析 题怎么说你怎么做 没有坑点 代码总览 #include <iostream> #include & ...
- Python通过PhantomJS获取JS渲染后的网页源代码
新建一个文件,命名为test.js,内容如下: var page = require('webpage').create(), system = require('system'), address; ...
- Java编程MapReduce实现WordCount
Java编程MapReduce实现WordCount 1.编写Mapper package net.toocruel.yarn.mapreduce.wordcount; import org.apac ...
- ACE线程管理机制-并发控制(3)
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/04/581854.html ACE Condition类属 ACE Condition类属(条 ...
- 轻量级HTTP服务器Nginx(配置与调试Nginx)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ixdba.blog.51cto.com/2895551/790611 Nginx ...
- 关于jmf不能播放mp3的问题解决
想写个JAVA的MP3音乐管理器,使用JMF插件,但发现运行时总报一个异常: Unable to handle format: mpeglayer3, 44100.0 Hz, 16-bit, Ster ...
- 持续集成之配置环境创建JOB
1.安装mvn.gitlab插件 2.配置参数构建
- 不使用Tomcat,手写简单的web服务
背景: 公司使用的YDB提供了http的查询数据库服务,直接通过url传入sql语句查询数据-_-||.ydb的使用参照:https://www.cnblogs.com/hd-zg/p/7115112 ...
- 【poj1830-开关问题】高斯消元求解异或方程组
第一道高斯消元题目~ 题目:有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关 ...