1250: Gobonacci

时间限制: 1 Sec  内存限制: 128 MB
提交: 75  解决: 11
[提交][状态][讨论版]

题目描述

Fibonacci sequence is well-known to us ,f(0)=1,f(1)=1, f(n) = f(n-1)+f(n-2), how nice the recursion! In addition, the is exponential growth in series. Obviously, Goagain was not content with the fact that the series didn’t increased as rapidly as he had expected, therefore, he designed such a function

g(0) = a   g(1)= b    g(2)=c

g(n)=3*g(n-1)+2*g(n-2)+g(n-3)      (n>=2)

Besides, the function was named after Goagain series. What made him distracted was that he could hardly calculate the series.

By the way , goagain without any festival fuck named it Gobonacci sequence

输入

Multiple sets of input data, each set including four positive integers a, b, c ,n  ( a, b, c <= 100 , 0<=  n <2^60)

输出

Please output the value of g(n), considering the value of g(n) is far too large, output g(n) %100000007  is okay.

样例输入

1 1 1 33 2 1 3

样例输出

610
#include<iostream>
using namespace std;
struct m{long long  int a[3][3]; };
long long int big = 100000007;
m aa = { 0, 1, 0, 0, 0, 1, 1, 2, 3 };
m mul(m a, m b){int i, j, k;m c;for (i = 0; i < 3;i++)for (j = 0; j < 3; j++){c.a[i][j] = 0;for (k = 0; k < 3; k++)c.a[i][j] +=( (a.a[i][k]%big) * (b.a[k][j]%big))%big;c.a[i][j] %= big;}return c;
}
m power(m a, long long int n){if (n == 1)return a;m b = power(a, n / 2);m c = mul(b, b);if (n % 2 == 1)c = mul(a, c);return c;
}
int main(){freopen("in.txt", "r", stdin);long long int a, b, c, n;while (cin >> a >> b >> c >> n){if (n == 0){ cout << a << endl; continue; }if (n == 1){ cout << b << endl; continue; }if (n == 2){ cout << c << endl; continue; }m ans = power(aa, n-2);cout << (ans.a[2][0] * a + ans.a[2][1] * b + ans.a[2][2] * c) % big << endl;}
}

转载于:https://www.cnblogs.com/weiyinfu/p/5013903.html

矩阵乘法+快速幂+序列递推公式相关推荐

  1. 求解斐波那契第n项的几种解法(含矩阵乘法+快速幂) Python实现

    斐波那契数列 首先我们来定义一下斐波那契数列: f(n)={0n = 01n = 1f(n−1)+f(n−2)n > 1f(n)= \begin{cases} 0 & \text {n ...

  2. [矩阵乘法/快速幂专题]Arc of Dream,Recursive sequence,233 Matrix,Training little cats

    矩阵快速幂习题 复习矩阵乘法及快速幂模板 乘法模板 快速幂模板 T1:Arc of Dream 题目 题解 code T2:Recursive sequence 题目 题解 code T3:233 M ...

  3. ZCMU-1618-骨牌覆盖(矩阵乘法+快速幂)

    1618: 骨牌覆盖1 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 264  Solved: 124 [Submit][Status][Web Bo ...

  4. Xn数列(矩阵乘法+快速幂+慢速乘法)

    Xn数列 题目描述: 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输入描述: 一行 ...

  5. 【BZOJ 2323】 2323: [ZJOI2011]细胞 (DP+矩阵乘法+快速幂*)

    2323: [ZJOI2011]细胞 Description 2222年,人类在银河系外的某颗星球上发现了生命,并且携带了一个细胞回到了地球.经过反复研究,人类已经完全掌握了这类细胞的发展规律: 这种 ...

  6. Luogu P3597 [POI2015]WYC___矩阵乘法快速幂+倍增

    题目大意: 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长度,注意路径不一定是简单路径,即可以重复走同一个点. 无自环 ...

  7. bzoj4887: [Tjoi2017]可乐(矩阵乘法+快速幂)

    题目传送门 . 解法: 这种题都是矩阵乘法吧. f[i][t]表示第t秒在i的方案. 那么f[i][t]可以转移f[i][t+1]和能够与i相连的边. 那么这个转移可以看作一个转移矩阵. 首先矩阵的( ...

  8. 1250 Fibonacci数列(矩阵乘法快速幂)

    1250 Fibonacci数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 定义:f0=f1=1, fn=fn-1 ...

  9. srm#397_div1_500pt 矩阵乘法+快速模幂

    题目地址:srm#397_div1_500 题目描述: Problem Statement   NOTE: This problem statement contains superscripts t ...

最新文章

  1. 各种flash的不同
  2. sort -k选项详解
  3. 玩转Autorun.inf
  4. 面向对象第七章,内存的管理,面向对象三大特征封装、继承、多态
  5. USACO翻译:USACO 2014 DEC Silver三题
  6. JAVA就业面试题之单例模式
  7. Eclipse Validating减少不必要的验证
  8. 新浪微博爬虫设计(Python版)
  9. 教你webpack、react和node.js环境配置(上篇)
  10. 2013 成都邀请赛
  11. Lingo 基本使用
  12. 数据挖掘--决策树ID3+k-means聚类分析西瓜数据
  13. 如何从表象深入IPFS本质?Filenet诠释公链底层难题
  14. CS144 Lab1:StreamReAssemer
  15. 编程初学者的那些痛楚
  16. [SAP ABAP开发技术总结]增强Enhancement
  17. Cesium 概述 (一)
  18. 蓝肽子序列 [蓝桥杯]
  19. 第7章第29节:四图排版:四张图片并列排版 [PowerPoint精美幻灯片实战教程]
  20. 线路板PCB产品和标准简介

热门文章

  1. 2019-02-25 SQL:cast(itemvalue as decimal(19,4))
  2. P2479 [SDOI2010]捉迷藏
  3. 1090 Highest Price in Supply Chain (25 分)(模拟建树,找树的深度)牛客网过,pat没过...
  4. 设置mysql密码 Access denied 问题
  5. Python之Mac上搭建集成开发环境
  6. 读书笔记《React:引领未来的用户界面开发框架》
  7. samba服务器的搭建【在windows查看linux的共享文件】
  8. pythonNumpy元素特定条件查找过滤[博]
  9. csv导入sqlite(python)
  10. 明确职责分工的重要性_发挥属地作用明确责任分工