The Tribonacci sequence is the extension of the Fibonacci Gschirnwirt sequence.
It's interesting that we can find
This is some in-depth study of the Tribonacci sequence.
Here is the time to paste the code:
Solution One (semi-product)
This method does not explain, do not understand to see the most stupid method recursive solution, and this is the optimization of recursive solution
Import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner Scanner = new Scan NER (system.in); while (Scanner.hasnext ()) {Long L = Scanner.nextlong (); Long r = Scanner.nextlong (); Long sum = 0; sum = Sum_tribonacci (l, R); SYSTEM.OUT.PRINTLN (sum% 1000000007);//for (long i = 0; I <= 100l; i++) {//sum = Sum_tribonacci (0, I);//System.out.println (i + "---------------------------" + sum//% 1000000007l);// }}} public static long Sum_tribonacci (long L, long R) {Long N1 = 0, N2 = 0, n3 = 0; Long U1, U2; Long sum = 0; if (R < 3) {return (r-l + 1); } n1 = 0; N2 = 1; N3 = 2; N1 = 1; N2 = 1; N3 = 1; Long N4 = 0; if (L < 3) {//for (long i = 3; I <= R; i++) {for (Long i = 3; I <= R + 1; i++) {N4 = n3 + n2 + N1; N1 = n2; N2 = n3; N3 = N4; } sum = n3-l; return sum; } else {//for (long i = 3; I <= l; i++) {for (Long i = 3; I <= l; i++) {N4 = n3 + n2 + N1; N1 = n2; N2 = n3; N3 = N4; } long sum1 = n3; SUM+=N4; for (Long i = l + 1; I <= r + 1; i++) {N4 = n3 + n2 + N1; N1 = n2; N2 = n3; N3 = N4; } Long sum2 = n3; return SUM2-SUM1; } }}
Solution Two
So the key here is to ask for the matrix A.
The matrix can be calculated using the first 5 items (series {1, 2, 3, 6, 11 ...} According to test instructions This is the sequence {1, 1, 1, 3, 5 ...} The n-1 of the former and still satisfy the Tribonacci rule)
Here the n power of the matrix, I use the dichotomy.
1 Import java.math.BigDecimal; 2 Import Java.util.Scanner; 3 4 public class CopyOfMain2 {5 public static BigDecimal i1000000007 = new BigDecimal (6 String.value Of (1000000007)); 7//defines four numbers here, in fact to prepare for the initialization of the following BigDecimal array 8 public static BigDecimal i3 = new BigDecimal (string.valueof (3)); 9 public static BigDecimal i2 = new BigDecimal (string.valueof (2)); Ten public static BigDecimal I1 = new BigDecimal (string.valueof (1)); public static BigDecimal I0 = new BigDecimal (string.valueof (0)); public static void Main (string[] args) {Scanner Scanner = new Scanner (system.in); Ile (Scanner.hasnext ()) {n-Long L = Scanner.nextlong (); r = Scanner.nextlong (); 20 bigdecimal[] sum = {I0,i0}; sum = Tribonacci (l, R). Divideandremainder (i1000000007); if (Sum[1].longvalue () <0) System.out.println (Sum[1].add (i1000000007)); ELSE SYSTEM.OUT.PRINTLN (SUM[1]); 26//This is a relatively stupid method, with this kind of traversal of the sum of course will be timed out (long i = l; I <= R; i++) {//sum + = Tribonacci (i); +//}//for (long I =0; I <= 100l; i++) {//Sum=tribonacci (0,i); 32 System.out.println (i+ "---------------------------" +sum%//1000000007l); (+/} +})-PNs public static BigDecimal Tribonacci (long L, long R) {BIGD Ecimal sum = i0; if (R < 3) {<= for (long i = l; i + R; i++) {sum = new BigDecimal (strin G.valueof (r-l + 1)); The sum of the return sums; * Bigdecimal[][] base = {{I1, i1, I0}, {i1, I0, I1}, {i1, I0, I0}}; if (l >= 3l) {bigdecimal[][] res1 = Matrixpower (base, l-3); bigdecimal[ [] res = Matrixpower (base, r-2); 51//long[][] res = Mulimatrix (Res1,matrixpower (base, r-l+1)); Res[0][0].subtract (res1[0][0]). Multiply (i3). Add (Res[1][0].subtract (res1[1 ][0]). Multiply (I2)). Add ((Res[2][0].subtract (res1[2][0))); () else {bigdecimal[][] res = Matrixpower (base, r-2); return (RES[0][0].MULTIPL Y (i3). Add (Res[1][0].multiply (I2)). Add (Res[2][0]). Subtract (New BigDecimal (String.valueof (l))); 59} 60 61} 62//Tribonacci The method of each item of the sequence number/public static long Tribonacci (long N) {//if (n = = 0l | | n = = 1l | | n = = 2l) {//return 1; +/} else if (n = = 3l)//return 3; //long[][] base = {{1l, 1l, 0l}, {1l, 0l, 1l}, {1l, 0l, 0l}}; //long sum = 0l; +/////long[][] res = Matrixpower (base, n-3l); //////Return 3l * res[0][0] + res[1][0] + res[2][0]; BIGD/}Ecimal[][] Matrixpower (bigdecimal[][] base, long p) {bigdecimal[][] res = new Bigdecimal[base.length][base[0]. Length]; (int i = 0; i < res.length; i++) {j< for (int j = 0; res[0].length; j + +) {80 if (i = = j) {Bayi res[i][j] = I1; I0; BigDecimal tmp[][] = base; The (; p! = 0; p >>= 1) {if (P & 1)! = 0) {res = Mulimatrix (res, TMP); The Mulimatrix tmp = (TMP, TMP); 94 return res; [bigdecimal[][] Mulimatrix (bigdecimal[][] M1, 98 bigdecimal[][] m2) {99 bigdecimal[][] res = new bigdecimal[m1.length][m2[0].length];100 for (int i = 0; i < res.length; i++) {101 for (int j = 0; j< res[0].length; j + +) {102 resI [j] = i0;103}104}105 for (int i = 0; i < m1.length; i++) {106 for (int j = 0; J < M2[0].length; J + +) {107 for (int k = 0; k < m2.length; k++) {108 109 res[i][j] = Res[i][j].ad D (M1[i][k].multiply (M2[k][j])). Divideandremainder (i1000000007) [1];110 111}112}113}1 Return res;115 116}117 118}
Tribonacci (Tybalt) The solution problem of the first n of the sequence number