Akash is feeling lonely.So he decided to visit his friend's house.
His friend's house is located at the distance of K metres from his
house.Now,he can take a step of one metre ,two metres or three
metres at a time.Can you tell me number of ways of reaching
Akash's friend house.
As the answer can be large Print answer mod 1000000007
Input
First line contains an integer t denoting number of test cases
Next t lines contains an integer k
Output
Print t lines containing number of ways to to reach friend's house modulo 1000000007
SAMPLE INPUT
2
2
3
SAMPLE OUTPUT
2
4
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to compute the number of ways to reach distance K using steps of 1,2,3 meters. That's essentially tribonacci numbers: number of ways to climb n stairs with steps 1,2,3: f(0)=1 (empty combination), f(1)=1, f(2)=2, f(3)=4, f(4)=7, etc. The recurrence: f(n) = f(n-1)+f(n-2)+f(n-3) for n>=3.
Given t test cases and each k up to? Not specified, but likely large (maybe up to 10^18? Not sure). Since mod is 1e9+7, we can compute for each test case individually. But if k is large (like 10^18) we'd need matrix exponentiation to compute f(k) quickly. However the problem statement is simple; typical DP up to 10^6 might be fine. But we should examine constraints? Not provided. Usually these problems have constraints like 1 ≤ t ≤ 100, 1 ≤ k ≤ 10^5 or 10^6. But sometimes they could be up to 10^18 requiring fast exponentiation. Let's check: The problem title? "Akash is feeling lonely" seems like from some platform like HackerEarth. I recall a problem with steps of 1,2,3 and answer mod 1e9+7. Usually constraints: t up to 10, k up to 10^5. But sometimes they ask for up to 10^6. To be safe, we can implement both? But given typical Codeforces/HackerEarth problem, it's fine to compute DP up to maximum k among test cases. However if k can be up to 10^12? Then DP would be too slow. However the problem statement doesn't give constraints; to be generic, we can implement matrix exponentiation that works for any k up to 10^18. Usually t is small. So we can precompute up to some max? But better to use matrix exponentiation.
We need to output number of ways mod 1e9+7.
Edge cases: k = 0? Probably k >= 1? But sample includes 2 and 3. If k=0, number of ways = 1 (do nothing). Usually they define distance K metres, maybe K>0. But we can handle k=0.
We'll compute using fast exponentiation with matrix [[1,1,0],[1,0,1],[1,0,0]] or similar. Let's define f(0)=1, f(1)=1, f(2)=2. Then recurrence f(n)=f(n-1)+f(n-2)+f(n-3). For n=1: f(1)=f(0)+f(-1)+f(-2)? Not valid. So we need base values: f(0)=1, f(1)=1, f(2)=2.
We can represent vector [f(n), f(n-1), f(n-2)]^T. Then next vector [f(n+1), f(n), f(n-1)] = M * [f(n), f(n-1), f(n-2)] with M = [[1,1,1],[1,0,0],[0,1,0]]. Check: f(n+1)=f(n)+f(n-1)+f(n-2); f(n) remains f(n); f(n-1) becomes f(n-1). But in vector order: v_n = [f(n), f(n-1), f(n-2)]^T. Then v_{n+1} = [f(n+1), f(n), f(n-1)]^T = [[1,1,1],[1,0,0],[0,1,0]] * [f(n), f(n-1), f(n-2)]^T. That works.
Alternatively use matrix [[1,1,0],[1,0,1],[1,0,0]]? Let's derive: suppose we want to compute f(n) from previous three. The typical transition: [ f(n) , f(n-1), f(n-2) ] = M * [ f(n-1), f(n-2), f(n-3) ]? That might be easier: let w_n = [f(n), f(n-1), f(n-2)]^T. Then w_{n} = A * w_{n-1} with A = [[1,1,1],[1,0,0],[0,1,0]]. Starting from n=2: w_2 = [f(2), f(1), f(0)] = [2,1,1]. For n=3, w_3 = A * w_2 = [2+1+1,2,1]=[4,2,1] correct. So A works.
Thus to compute f(k) for k>=2, we need to compute w_k = A^{k-2} * w_2 (if k>=2). For k=0,1,2 we return directly.
But careful: If we define f(0)=1, f(1)=1, f(2)=2, then for k=3 we need A^{1}*w_2 = w_3 = [4,2,1] -> f(3)=4. Good.
Thus algorithm:
Read t.
For each k:
if k == 0: print 1
elif k == 1: print 1
elif k == 2: print 2
else:
compute M = A^{k-2} mod MOD
result = (M[0][0]*f2 + M[0][1]*f1 + M[0][2]*f0) % MOD, where f2=2, f1=1, f0=1.
Print result.
Check: w_2 = [2,1,1]. Then w_k = A^{k-2} * w_2. So f(k) = first element of w_k = (first row of A^{k-2}) dot w_2.
Alternatively, we can compute using exponentiation on matrix 3x3.
We'll implement matrix multiplication modulo MOD.
Complexities: O(t * log k) with small constant.
Alternatively, if constraints are small, we could precompute DP up to max_k. But without constraints, safer to use matrix exponentiation.
Edge Cases: k may be large up to 10^18. Use long long (64-bit). Ensure modulo operations.
I'll implement fast exponentiation function for matrix.
Also note that the sample: Input: 2,2,3 -> Output: 2,4. For k=2 -> 2, k=3 -> 4. Works.
Thus solution: Use matrix exponentiation.
Implementation details:
Define const long long MOD = 1000000007;
struct Mat {
long long a[3][3];
Mat() { memset(a,0,sizeof a); }
Mat identity() { Mat I; for(int i=0;i<3;i++) I.a[i][i]=1; return I; }
Mat operator*(const Mat& other) const {
Mat res;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
long long sum=0;
for(int k=0;k<3;k++)
sum = (sum + a[i][k]*other.a[k][j]) % MOD;
res.a[i][j] = sum;
}
return res;
}
};
Mat power(Mat base, long long exp) {
Mat result = base.identity();
while(exp>0) {
if(exp&1) result = resultbase;
base = basebase;
exp>>=1;
}
return result;
}
In main: read t, loop:
long long k; cin>>k;
if(k==0) cout<<1<<endl;
else if(k==1) cout<<1<<endl;
else if(k==2) cout<<2<<endl;
else {
Mat M;
M.a[0][0]=1; M.a[0][1]=1; M.a[0][2]=1;
M.a[1][0]=1; M.a[1][1]=0; M.a[1][2]=0;
M.a[2][0]=0; M.a[2][1]=1; M.a[2][2]=0;
Mat P = power(M, k-2);
long long ans = (P.a[0][0]*2 + P.a[0][1]*1 + P.a[0][2]*1) % MOD;
cout<<ans<<endl;
}
Check: For k=3: k-2=1, P=M. P.a[0][0]=1, P.a[0][1]=1, P.a[0][2]=1. ans = 12 + 11 + 1*1 = 4. correct.
For k=4: k-2=2, M^2 =? M^2: compute:
M = [[1,1,1],[1,0,0],[0,1,0]].
M^2 = MM:
Row0: (11+11+10=2), (11+10+11=2), (11+10+10=1) -> [2,2,1]
Row1: (11+01+00=1), (11+00+01=1), (11+00+00=1) -> [1,1,1]
Row2: (01+11+00=1), (01+10+01=0), (01+10+00=0) -> [1,0,0]
So P = M^2, ans = 22 + 21 + 1*1 = 4+2+1=7. f(4)=7, correct.
Thus matrix exponentiation works.
If the test cases contain many queries with large k, this is efficient.
We could also precompute using DP if we know max_k is <= maybe 10^6. But matrix exponentiation is fine.
Potential pitfalls: large t (like 10^5) and each k up to 10^18 -> t*log k = 10^5 * 60 = 6e6, which is okay. But reading/writing also fine.
Thus final answer.
Now write solution in C++.
I'll produce a clean code with fast I/O perhaps.
Also note: The problem might require handling K=0? Not sure, but we'll include.
Edge case: When k=0, output 1 (empty step). If friend's house is at distance 0, he is already there, so 1 way.
But sample doesn't cover.
Let's also verify: if k=1, output 1; k=2 output 2; k=3 output 4.
Alright.
Now output to each test case.
Thus final solution. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
// 3x3 matrix for tribonacci recurrence
struct Matrix {
ll a[3][3];
Matrix() { memset(a, 0, sizeof a); }
Matrix identity() const {
Matrix I;
for (int i = 0; i < 3; ++i) I.a[i][i] = 1;
return I;
}
Matrix operator*(const Matrix& other) const {
Matrix res;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) {
ll sum = 0;
for (int k = 0; k < 3; ++k)
sum = (sum + a[i][k] * other.a[k][j]) % MOD;
res.a[i][j] = sum;
}
return res;
}
Matrix power(ll exp) const {
Matrix base = *this;
Matrix result = identity();
while (exp > 0) {
if (exp & 1) result = result * base;
base = base * base;
exp >>= 1;
}
return result;
}
};
ll solve(ll n) {
if (n == 0) return 1;
if (n == 1) return 1;
if (n == 2) return 2;
// matrix M such that [f(i), f(i-1), f(i-2)]^T = M * [f(i-1), f(i-2), f(i-3)]^T
Matrix M;
M.a[0][0] = 1; M.a[0][1] = 1; M.a[0][2] = 1;
M.a[1][0] = 1; M.a[1][1] = 0; M.a[1][2] = 0;
M.a[2][0] = 0; M.a[2][1] = 1; M.a[2][2] = 0;
Matrix P = M.power(n - 2);
ll ans = (P.a[0][0] * 2 + P.a[0][1] * 1 + P.a[0][2] * 1) % MOD;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);