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 ...
随机推荐
- 【bzoj2743】[HEOI2012]采花 树状数组
题目描述 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于公 ...
- [Leetcode] word search 单词查询
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- SGU - 282
SGU - 282 题解 题意: 本质不同的集合:不存在两个方案重新编号之后对应的边集相同(对于所有x,y,,(x,y)边颜色都相同). (1≤ N≤ 53, 1≤ M≤ 1000) 对P取模 本质不 ...
- Java 匿名内部类 只能访问final变量的原因
文章来源:http://blog.sina.com.cn/s/blog_4b6f8d150100qni2.html 1)从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方 ...
- 被引用的外部JS存在window.onload时,判断当前页面是否已存在window.onload,并进行相应处理
如果页面a.html引用了b.js,b.js里的方法需要在页面资源加载完成后执行,即在window.onload里执行:这时如果a.html里使用了window.onload方法,b.js就不能重复调 ...
- Ubuntu16.04+Cuda8.0+1080ti+caffe+免OpenCV3.2.0+faster-rCNN教程
一.事先声明:1.Ubuntu版本:Ubuntu使用的是16.04.而不是16.04.1或16.04.2,这三个是有区别的.笔者曾有过这样的经历,Git上一个SLAM地图构建程序在Ubuntu14.0 ...
- AndroidManifest Ambiguity方案原理及代码
1简述 前段时间在bluebox的一份android安全pdf中看到一个AndroidManifest Ambiguity方案.该方案基于android系统解析AXML的一个特点:android在解析 ...
- 使用 Rational AppScan 保证 Web 应用的安全性,第 2 部分: 使用 Rational AppScan 应对 Web 应用攻击
1 当前 Web 安全现状 互联网的发展历史也可以说是攻击与防护不断交织发展的过程.目前,全球因特网用户已达 13.5 亿,用户利用网络进行购物.银行转账支付和各种软件下载,企业用户更是依赖于互联网构 ...
- Java中x=x+1 与x+=1 的一点区别
转载自:http://www.cnblogs.com/heshan664754022/archive/2013/04/01/2994028.html 作者:十年半山 今天同悦姐学到了关于Java的复合 ...
- Java设计模式の观察者模式(推拉模型)
目录: 一.观察者定义 二.观察者模式的结构(推模式实现) 三.推模型和拉模型(拉模式实现) 四.JAVA提供的对观察者模式的支持 五.使用JAVA对观察者模式的支持(自带推模式实现实例) 一.观察者 ...