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 ...
随机推荐
- 洛谷3690:【模板】Link Cut Tree——题解
https://www.luogu.org/problemnew/show/P3690 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- 洛谷 P3521 [POI2011]ROT-Tree Rotations 解题报告
P3521 [POI2011]ROT-Tree Rotations 题意:递归给出给一棵\(n(1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 大体 ...
- Linux之根文件系统介绍与分析20160611
说一下LINUX根文件系统的介绍与分析: 1.内核启动应用程序,首先要识别出应用程序,这时就需要文件系统来帮助内核找到对应的应用程序: 2.第一个启动的应用程序就是sbin目录下的init程序,也就是 ...
- JavaScript对iframe的DOM操作
在IE6.IE7中,我们可以使用 document.frames[ID].document 来访问iframe子窗口中的document对象,可是这是不符合W3C标准的写法,也是IE下独有的方法,在F ...
- mysql 常用总结
centos7 安装mysql 数据库安装参考:http://www.cnblogs.com/longrui/p/6071581.htmlhttps://www.cnblogs.com/yoursou ...
- mysql的concat用法
问题提出:mybatis的mapper文件中的模糊查询: mysql CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一,下面就将为您详细介绍mysql CONCAT( ...
- centOS升级部分功能后,不能进入桌面(桌面)
在Linux中安装nginx,安装过程中需要安装c++等一部分环境,装完后,莫名奇妙的centos就不能进入桌面了,命令窗口可以进去. 网上查了查资料,说是升级了一部分功能,和内核有冲突.需要更新下系 ...
- python---异步IO(asyncio)协程
简单了解 在py3中内置了asyncio模块.其编程模型就是一个消息循环. 模块查看: from .base_events import * from .coroutines import * #协程 ...
- transition和animation概况
有人可能会有疑问,CSS3动画不是只有animation一个属性吗?怎么又和转化(transform)和过渡(transition)扯上关系了,其实并非如此,转化(transform)属性让动画的变换 ...
- Java——Iterate through a HashMap
遍历Map import java.util.*; public class IterateHashMap { public static void main(String[] args) { Map ...