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 ...
随机推荐
- 将 Objective-C 代码迁移到 Swift(Swift 2.0更新)-b
本节内容包括: 为你的Objective-c代码做好迁移准备 (Preparing Your Objective-C Code for Migration) 迁移过程(The Migration Pr ...
- cf B. Making Sequences is Fun
http://codeforces.com/contest/373/problem/B 用二分枚举长度就可以. #include <cstdio> #include <cstring ...
- Delphi新语法
http://www.cnblogs.com/hnxxcxg/category/456344.html
- redis 扩展安装使用
Redis的php客户端库非常之多, Redis推荐客户端链接是:http://redis.io/clients 推荐用phpredis,下载地址:https://github.com/nicolas ...
- require js 将config和入口函数分开写
原文地址 https://github.com/jrburke/requirejs/issues/354 Area there any plans to standardize/recommend a ...
- [转]百度地图点聚合MarkerClusterer移动地图时,Marker的Label丢失的问题
参考文献:http://www.cnblogs.com/jicheng1014/p/3143859.html 问题现象: 使用MarkerClusterer_min.js,可以实现点聚合,但是当将带有 ...
- POJ3273:Monthly Expense(二分)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- Swift学习笔记 - 函数与闭包
import Foundation //1.函数的定义与调用//以 func 作为前缀,返回箭头 -> 表示函数的返回类型func sayHello(name: String) -> St ...
- android如何让service不被杀死
1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建 @Override public int onStartCom ...
- Android开发/源代码资源汇总
全网最全的Android游戏源代码汇总 mod=viewthread&tid=5080&fromuid=5" target="_blank">htt ...