tribonacci
Everybody knows Fibonacci numbers, now we are talking about the Tribonacci numbers:
T[0] = T[1] = T[2] = 1;
T[n] = T[n - 1] + T[n - 2] + T[n - 3] (n >= 3)
Given a and b, you are asked to calculate the sum from the ath Fibonacci number to the bth Fibonacci number, mod 1,000,000,007, that is (T[a] + T[a + 1] + ... + T[b]) % 1,000,000,007.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
;
;
__int64 N;
void multipy( __int64 a[MAX_N][MAX_N], __int64 b[MAX_N][MAX_N], __int64 c[MAX_N][MAX_N] ){
; i <= ; i++ ){
; j <= ; j++ ){
c[i][j] = ;
; k <= ; k++ ){
c[i][j] = ( c[i][j] + a[i][k] * b[k][j] % MOD ) % MOD;
}
}
}
}
void get_matrix_pow( __int64 a[MAX_N][MAX_N], __int64 n ){
__int64 ans[MAX_N][MAX_N] = {};
__int64 temp[MAX_N][MAX_N];
; i <= ; i++ ) ans[i][i] = ;
while( n ){
== ){
multipy( ans, a, temp );
memcpy( ans, temp, sizeof( __int64 ) * MAX_N * MAX_N );
}
multipy( a, a, temp );
memcpy( a, temp, sizeof( __int64 ) * MAX_N * MAX_N );
n /= ;
}
memcpy( a, ans, sizeof( __int64 ) * MAX_N * MAX_N );
}
__int64 solve( __int64 n ){
__int64 a[MAX_N][MAX_N] = {};
){
;
}
){
;
} ){
;
}
a[][] = ;a[][] = ;a[][] = ;a[][] = ;
a[][] = ;a[][] = ;a[][] = ;a[][] = ;
a[][] = ;a[][] = ;a[][] = ;a[][] = ;
a[][] = ;a[][] = ;a[][] = ;a[][] = ;
get_matrix_pow( a, n - );
__int64 ans = ;
__int64 b[MAX_N];
b[] = b[] = b[] = ;
b[] = ;
; i <= ; i++ ){
ans = ( ans + a[][i] * b[i] % MOD ) % MOD;
}
return ans;
}
int main(){
__int64 A, B;
while( scanf( "%I64d%I64d", &A, &B ) != EOF ){
printf( ) + MOD ) % MOD );
}
;
}
tribonacci的更多相关文章
- [LeetCode] 1137. N-th Tribonacci Number
Description e Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + ...
- 【Leetcode_easy】1137. N-th Tribonacci Number
problem 1137. N-th Tribonacci Number solution: class Solution { public: int tribonacci(int n) { ) ; ...
- LeetCode.1137-第N个泰波那契数(N-th Tribonacci Number)
这是小川的第409次更新,第441篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第260题(顺位题号是1137).Tribonacci(泰波那契)序列Tn定义如下: 对于n&g ...
- 1137. N-th Tribonacci Number(Memory Usage: 13.9 MB, less than 100.00% of Python3)
其实思路很简单,套用一下普通斐波那契数列的非递归做法即可,不过这个成绩我一定要纪念一下,哈哈哈哈哈 代码在这儿: class Solution: def tribonacci(self, n: int ...
- 【leetcode】1137. N-th Tribonacci Number
题目如下: The Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 ...
- 【LeetCode】1137. N-th Tribonacci Number 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)
题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3); 求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...
- UVA12470—Tribonacci (类似斐波那契,简单题)
题目链接:https://vjudge.net/problem/UVA-12470 题目意思:我们都知道斐波那契数列F[i]=F[i-1]+F[i-2],现在我们要算这样的一个式子T[i]=T[i-1 ...
- Python简单试题
1,相乘次数 题目要求描述: 一个整数每一位上的数字相乘,判断是否为个位数,若是则程序结束 ,不是则继续相乘,要求返回相乘次数. 例:39 > 3*9=27 > 2*7=14 > 1 ...
随机推荐
- poj 1066 Treasure Hunt
http://poj.org/problem?id=1066 #include <cstdio> #include <cstring> #include <cmath&g ...
- 第23章 COM和ActiveX(COM可以实现跨进程跨机器的函数调用)
控件对象既可在EXE中实现,也可在DLL中实现.这种实现对于COM对象的用户来说是透明的.因为COM提供了调度服务(marshaling).COM调度机制能够化进程甚至跨机器的函数调用,这使得16位程 ...
- 查看堵塞的SQL
SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, r.trx_query waiting_query, b.t ...
- 【HDOJ】3587 NUDOTA
字符串模拟水题. /* 3587 */ #include <iostream> #include <cstdio> #include <cstring> #incl ...
- Delphi 写日志的类
unit uProgLog; interface uses Windows, SysUtils, SyncObjs; const C_LOG_LEVEL_TRACE = $; C_LOG_LEVEL_ ...
- 在Visual Studio中使用AStyle
最近在做一个C++项目,我们使用了一个叫做AStyle的插件来做代码格式化. 下载方式1:通过Visual Studio下载 启动Visual Studio,以下简称VS: 英文版VS:VS主菜单 & ...
- 一个简单的JUnit项目
本人一直很喜欢JAVA,可是真正接触到JUnit也不过半年.由于公司进行网页测试,采用的是 JUnit+selenium的方式搭建的测试框架,然后采用JAVA语言编写,所以本人也好好研究了一下JUni ...
- 封装实现UIButton左文字右图片
#import "TitleButton.h" @implementation TitleButton - (instancetype)initWithFrame:(CGRect) ...
- linux下的type命令
type命令用来显示指定命令的类型.一个命令的类型可以是如下几种: alias 别名 keyword 关键字,Shell保留字 function 函数,Shell函数 builtin 内建命令,She ...
- Java获取文件大小的正确方法(转)
Java中获取文件大小的正确方法 2014-03-28 14:03 64507人阅读 评论(9) 收藏 举报 分类: Java笔记(36) 研究成果(42) 版权声明:本文为博主原创文章,未经博 ...