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 ...
随机推荐
- setNeedsDisplay 和 setNeedsLayout-b
setNeedsDisplay: Marks the receiver’s entire bounds rectangle as needing to be redrawn. drawRect: Th ...
- Struts2+JQuery发送Ajax请求
Action类代码: package com.example.action; public class JsonAction { private String name; private int id ...
- android 休眠唤醒机制分析(三) — suspend
本文转自:http://blog.csdn.net/g_salamander/article/details/7988340 前面我们分析了休眠的第一个阶段即浅度休眠,现在我们继续看休眠的第二个阶段 ...
- 14.3 InnoDB Multi-Versioning InnoDB 多版本
14.3 InnoDB Multi-Versioning InnoDB 多版本 InnoDB 是一个多版本的存储引擎,它保持信息关于改变的数据老版本的信息, 为了支持事务功能比如并发和回滚. 这些信息 ...
- 7.5.1 Point-in-Time Recovery Using Event Times 使用Event Times 基于时间点恢复
7.5.1 Point-in-Time Recovery Using Event Times 使用Event Times 基于时间点恢复 表明开始和结束时间用于恢复, 指定 --start-datet ...
- 7.2 Database Backup Methods 数据备份方法:
7.2 Database Backup Methods 数据备份方法: 本节总结了一些常用的备份方法: 使用MySQL Enterprise Backup 进行Hot Backup MySQL Ent ...
- 【HDOJ】5017 Ellipsoid
简单地模拟退火. /* 5017 */ #include <cstdio> #include <cstring> #include <cstdlib> #inclu ...
- fg bg 等命令
fg.bg.jobs.&.ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的 一.& 最经常被用到 这个用在一个命令的最后,可以把这个命令 ...
- Hive学习笔记【转载】
本文转载自:http://blog.csdn.net/haojun186/article/details/7977565 1. HIVE结构 Hive 是建立在 Hadoop 上的数据仓库基础构架. ...
- 【2013杭州区域赛】部分题解 hdu4770—4780
1008: 题意: 有20W个数,每个数都在20W以内,现在有20W个询问,每次询问L,R区间内不会打架的数有多少个 定义两个数不互质 就会打架 解法: 脑洞很大的一道题,先要进行预处理,对每一个数预 ...