|
| 1 | +/* |
| 2 | +
|
| 3 | +The fibonacci sequence is defined by the following relation: |
| 4 | +
|
| 5 | +F(0) = 0 |
| 6 | +
|
| 7 | +F(1) = 1 |
| 8 | +
|
| 9 | +F(N) = F(N - 1) + F(N - 2), N >= 2 |
| 10 | +
|
| 11 | + |
| 12 | +
|
| 13 | +Your task is very simple. Given two non-negative integers N and M, you have to calculate the sum (F(N) + F(N + 1) + ... + F(M)) mod 1000000007. |
| 14 | +Input |
| 15 | +The rst line contains an integer T (the number of test cases). Then, T lines follow. Each test |
| 16 | +case consists of a single line with two non-negative integers N and M. |
| 17 | +
|
| 18 | + |
| 19 | +
|
| 20 | +The first line contains an integer T (the number of test cases). Then, T lines follow. Each test case consists of a single line with two non-negative integers N and M. |
| 21 | +Output |
| 22 | +
|
| 23 | +For each test case you have to output a single line containing the answer for the task. |
| 24 | +Example |
| 25 | +
|
| 26 | +Input: |
| 27 | +3 |
| 28 | +0 3 |
| 29 | +3 5 |
| 30 | +10 19 |
| 31 | +
|
| 32 | +Output: |
| 33 | +4 |
| 34 | +10 |
| 35 | +10857 |
| 36 | +
|
| 37 | +Constraints |
| 38 | +
|
| 39 | + T <= 1000 |
| 40 | + 0 <= N <= M <= 109 |
| 41 | +
|
| 42 | +
|
| 43 | +ALGORITHM: |
| 44 | +
|
| 45 | +Facts. |
| 46 | +
|
| 47 | +1. Fib(0) =0 ; Fib(1) =1; |
| 48 | +2. Sum of Fib(n) = (Fib(n+2) - 1) -> Fib(1) |
| 49 | +3. Sum of Fib( n to m) = Fib(m+2) - Fib(n+1) |
| 50 | +
|
| 51 | +Matrix Expo : |
| 52 | +
|
| 53 | +1 1 1 1 |
| 54 | +1 0 * 1 0 is Fib(2) |
| 55 | +
|
| 56 | +Fib(2) * Fib(2) is Fib(4) |
| 57 | +
|
| 58 | + 1 1 |
| 59 | +Fib(4) * 1 0 is Fib(5) |
| 60 | +
|
| 61 | +
|
| 62 | +Fib(n+1) Fib(n) |
| 63 | +Fib(n) Fib(n-1) |
| 64 | +
|
| 65 | +
|
| 66 | +Example : 13 |
| 67 | +
|
| 68 | +Multiply 1 2 4 8 Max . So 13-8 = 3. |
| 69 | +
|
| 70 | +or |
| 71 | +
|
| 72 | +Do conversion from decimal to binary . |
| 73 | +Result array will always contain updated value |
| 74 | +
|
| 75 | +Ex 5 and 6 101 and 110 |
| 76 | +
|
| 77 | +1 is added to 4 and 2 is added to 4 |
| 78 | +
|
| 79 | +*/ |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +# include <iostream> |
| 84 | +# include <string.h> |
| 85 | +typedef long long llong; |
| 86 | +using namespace std; |
| 87 | + |
| 88 | +const long long MOD = 1000000007; |
| 89 | + |
| 90 | +void multiply(llong first[2][2], llong second[2][2]) |
| 91 | +{ |
| 92 | + llong temp[2][2]; |
| 93 | + temp[0][0] = (first[0][0] * second[0][0] + first[0][1] * second[1][0]) % MOD ; |
| 94 | + temp[0][1] = (first[0][0] * second[0][1] + first[0][1] * second[1][1]) % MOD ; |
| 95 | + temp[1][0] = (first[1][0] * second[0][0] + first[1][1] * second[1][0]) % MOD ; |
| 96 | + temp[1][1] = (first[1][0] * second[0][1] + first[1][1] * second[1][1]) % MOD ; |
| 97 | + memcpy(first,temp,sizeof(temp)); |
| 98 | +} |
| 99 | + |
| 100 | +llong fibo(llong input) |
| 101 | +{ |
| 102 | + llong result[2][2]={{1, 0}, {0, 1}},current[2][2] = {{1, 1}, {1, 0}}; |
| 103 | + |
| 104 | + --input; |
| 105 | + |
| 106 | + while( input > 0 ) |
| 107 | + { |
| 108 | + if(input & 1) |
| 109 | + { |
| 110 | + multiply(result,current); |
| 111 | + } |
| 112 | + input>>=1; |
| 113 | + multiply(current,current); |
| 114 | + } |
| 115 | + |
| 116 | + return result[0][0]; |
| 117 | + |
| 118 | +} |
| 119 | +int main() |
| 120 | +{ |
| 121 | + int size; |
| 122 | + llong start,end,sum,first,second; |
| 123 | + cin>>size; |
| 124 | + for(int i=0; i<size; ++i) |
| 125 | + { |
| 126 | + cin>>start; |
| 127 | + cin>>end; |
| 128 | + first = fibo(end+2); |
| 129 | + second = fibo(start+1); |
| 130 | + sum = (first-second ) % MOD; |
| 131 | + if(sum < 0) |
| 132 | + { |
| 133 | + sum+=MOD; |
| 134 | + } |
| 135 | + cout<<sum<<endl; |
| 136 | + } |
| 137 | +} |
| 138 | + |
0 commit comments