题目链接:https://vjudge.net/problem/CodeForces-450B

B. Jzzhu and Sequences
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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).

Input

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

Output a single integer representing fn modulo 1000000007 (109 + 7).

Examples
input
2 3
3
output
1
input
0 -1
2
output
1000000006
Note

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[n] = f[n-1] - f[n-2],构造矩阵如下:

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; const int Size = ;
struct MA
{
LL mat[Size][Size];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA mul(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%MOD, ret.mat[i][j] %= MOD;
return ret;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
} MA tmp = {
, -,
,
}; int main()
{
LL f[], n;
while(scanf("%lld%lld%lld",&f[],&f[],&n)!=EOF)
{
if(n<=)
{
printf("%lld\n", (f[n]+MOD)%MOD);
continue;
} MA s = tmp;
s = qpow(s, n-);
LL ans = (1LL*s.mat[][]*f[]%MOD+1LL*s.mat[][]*f[]%MOD+*MOD)%MOD;
printf("%lld\n", ans);
}
}

CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂的更多相关文章

  1. POJ 3070(求斐波那契数 矩阵快速幂)

    题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace ...

  2. hdu4549 M斐波那契数列 矩阵快速幂+快速幂

    M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的 ...

  3. poj3070 (斐波那契,矩阵快速幂)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9630   Accepted: 6839 Descrip ...

  4. HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  5. 51nod1242 斐波那契数列 矩阵快速幂

    1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 #include<stdio.h> #define mod 100000000 ...

  6. POJ3070 斐波那契数列 矩阵快速幂

    题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...

  7. hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)

    Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...

  8. hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

  9. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

随机推荐

  1. 【面试】最容易被问到的N种排序算法!

    面试官:小明,是吧?你都知道哪些排序算法,哪几种是稳定排序? 小明:这个我有总结! 关于排序稳定性的定义 通俗地讲就是能保证排序前两个相等的数其在序列的前后位置顺序和排序后它们两个的前后位置顺序相同. ...

  2. Oracle中PL/SQL 范例

    1.写匿名块,输入三角形三个表的长度.在控制台打印三角形的面积 declare v_side_first ):=&第一条边; v_side_second ):=&第二条边; v_sid ...

  3. linux jar 命令使用

    原文链接:http://blog.chinaunix.net/uid-692788-id-2681136.html JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是 ...

  4. 321. Create Maximum Number

    /* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...

  5. 讯飞语音识别Android-Demo

    import java.io.UnsupportedEncodingException; import android.app.Activity; import android.os.Bundle; ...

  6. mac mysql忘记密码解决办法

    http://www.jb51.net/article/87580.htm http://blog.csdn.net/soft2buy/article/details/50223373

  7. 老毛桃winpe优盘启动系统个性修改全攻略.(全)

    博主从05年开始接触计算机,不能说是高手也算个老菜了,当时装系统还是用蕃茄花园的光盘安装系统,后来在学校管理机房,哪台电脑坏了就硬盘对拷. 时到今日,重启系统的方法五花八门,其中使用最广的莫过于PE优 ...

  8. 【Todo】Java学习路线(方向指导)

    在网上搜了下Java学习路线(关键词:学习,因为众所周知,实践出牛人,在平时工作不怎么深入的情况下,才强调学习的方向的重要性 ^_^) 发现下面知乎这个回答写的真好.mark如下: https://w ...

  9. mysql 控制台环境下查询中文数据乱码,插入、更新中文数据不成功

    mysql 控制台环境下查询中文数据乱码,插入.更新中文数据不成功         登录mysql密码是加入编码参数--default-character-set,中文用gbk mysql -uroo ...

  10. ubuntu下调试ffmpeg程序出现undefined reference to pthread_once ,undefined reference to uncompress错误

    Ubuntu(版本16.04)下默认配置编译Ffmpeg(版本4.1.3configure 添加选项--enable-threads),将编译好的ffmpeg库添加到程序 中进行编译出现undefin ...