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 ...
随机推荐
- HDOJ.2072 单词数(map)
单词数 点我挑战题目 点我一起学习STL-MAP 题意分析 给出一行单词,判断这行有不同种的单词多少个,用map可以轻松解决. 代码总览 /* Title:HDOJ.2072 Author:pengw ...
- linux查找文件目录及mysql卸载
我们要卸载 mysql但是不知道其安装在哪里了,可以用 where +关键词 的方式查找,如上图 输入 whereis mysql 后,下面显示出了4个包含mysql的位置. ..... 查看安装m ...
- Django 2.0 URL
Overview¶ A view is a “type” of Web page in your Django application that generally serves a specific ...
- [ethernet]ubuntu更换网卡驱动
问题: 网络不能ping通,dmesg显示很多 [::00.0: eth0: link up [::00.0: eth0: link up [::00.0: eth0: link up [::00.0 ...
- Redrain 通用菜单控件使用方法和说明(附源码和demo)
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42889709 大概半年前我写过博客说明怎么改造duilib的原代Menu ...
- IntelliJ IDEA使用技巧 (类比pycharm)
第1章 课程介绍 1)下载及安装2)界面介绍 ... ①界面中左右最边上的小窗口(旋转了90度的字体)都自带了下标,win下快捷键alt+index切换窗口 ②演示效果:模拟时钟,快捷键如,0到10 ...
- linux内存相关好文(转)
话说团队的兄弟有一天问我,为啥咱唯一的一个服务器,内存都用完了,我还想在上面测性能呢.我一听,第一反应:不可能!我说你胡扯呢吧,咱那可是16G的一个物理机,上面就跑了git服务器,怎么可能把内存吃完了 ...
- 「6月雅礼集训 2017 Day4」寻找天哥
[题目大意] 给出$n$个三维向量,设当前向量长度为$L$,每次沿着向量等概率走$[0,L]$个长度.一个球每秒半径增加1个长度,直到覆盖位置,每秒耗能为球体积,求总耗能的期望. 设最后半径为R,那么 ...
- .NET FrameWork 中的 CTS
CTS:Common Type System 通用类型系统. 1.不仅可以把C#编译成.Net IL,还支持Basic.Python.Ruby等语言,甚至还支持Java.不同语言中的数据类型定义是不一 ...
- 深入理解微服务架构spring的各个知识点(面试必问知识点)
什么是spring spring是一个开源框架,spring为简化企业级开发而生,使用spring可以使简单的java bean 实现以前只有EJG才能实现的功能. Spring是一个轻量级的控制反转 ...