We need to compute the number of sequences of length N (digits 0-9) on a standard phone keypad (3x4 grid: 1,2,3 on first row; 4,5,6 second; 7,8,9 third; and 0 usually below 8 on some keypads? Actually typical numeric keypad for mobile phones is like:
1 2 3
4 5 6
7 8 9
But they said cannot press * and #, so only digits 0-9. However, adjacency: digits are positions on grid. We need to define the adjacency for all digits including 0. In many phone keypads, 0 is at the bottom below 8, and sometimes also adjacent to * and # but we ignore those. Usually adjacency includes left, right, up, down. So we need to define which digits are adjacent to each other.
Standard keypad: layout:
Row1: 1 (col1), 2 (col2), 3 (col3)
Row2: 4 (col1), 5 (col2), 6 (col3)
Row3: 7 (col1), 8 (col2), 9 (col3)
Row4: * (col1), 0 (col2), # (col3)
We consider only digits 0-9. So we need adjacency for digits: 1,2,3,4,5,6,7,8,9,0.
From grid, each cell has up to 4 neighbors. For digits:
- 1: neighbors? left? none; right:2; up:none; down:4. So {2,4}
- 2: neighbors: left:1, right:3, up? none? Actually above 2 is nothing? Row1 top no; down:5. So {1,3,5}
- 3: neighbors: left:2, down:6. up:none; right:none => {2,6}
- 4: neighbors: up:1, down:7, right:5, left:none => {1,5,7}
- 5: neighbors: up:2, down:8, left:4, right:6 => {2,4,6,8}
- 6: neighbors: up:3, down:9, left:5, right:none => {3,5,9}
- 7: neighbors: up:4, down:*? Actually down is * (not a digit), but we only consider digits. Also right:8 => neighbors: {4,8}. Also maybe left? none. So {4,8}
- 8: neighbors: up:5, down:0, left:7, right:9 => {5,7,9,0}
- 9: neighbors: up:6, down:#? not digit; left:8, right:none => {6,8}
- 0: neighbors: up:8, down:#? not digit; left:? not digit; right:#? not digit. But also maybe left? Actually from grid, 0 at (4,2). Up:8, left: (ignore), right:# (ignore). So only neighbor digit: {8}. Some keypads have 0 adjacent to something else? But likely just {8}. However, sometimes 0 is considered adjacent to * and # but condition says cannot press * and #, so they are not allowed as next digit. So only neighbor digit is 8. Also, some interpretations might consider 0 also adjacent to 7? But no, that's not typical. So we will assume 0 only adjacent to 8 (and maybe 5? no). But wait, many phone keypads have 0 below 8, and 8 is above 0, also maybe left/right? There is no digit left or right. So yes, only 8.
But double-check: Some keypads have 0 at the bottom center, and maybe adjacent to 7,8,9? Actually if it's a 3x4 grid, row4: *,0,#. So left neighbor is *, right neighbor is #. So only digit neighbor is up:8. So correct.
Thus adjacency graph:
1: [2,4]
2: [1,3,5]
3: [2,6]
4: [1,5,7]
5: [2,4,6,8]
6: [3,5,9]
7: [4,8]
8: [5,7,9,0]
9: [6,8]
0: [8]
Now the problem: Count number of sequences of length N (allow repetitions) where each transition from digit i to digit j must be adjacent (i.e., j is in adjacency list of i). Also, any starting digit is allowed? The problem statement: "Given a number N, DJ boy has to tell how many numbers of length N are possible." It says "numbers of length N", implying sequences of digits length N. It doesn't restrict starting digit, but it's implied that you can start on any digit. Condition 2 says "He may press the same key again" which means staying on same digit is allowed if it's adjacent to itself? Wait: "He may press the same key again i.e. numbers like 000 & 080 are possible for length 3." For 000 to be possible, that means from 0 you can press 0 again, meaning 0 must be considered adjacent to itself? But adjacency defined as left, right, up, bottom. The same key is not adjacent to itself (same position). However, condition 2 explicitly says "He may press the same key again", so we allow repetition of the same digit, i.e., staying in place is allowed. That means that in the graph we should add self-loops for all digits (i.e., from any digit you can go to itself). Because they say numbers like 000 are possible, which would require that from 0 you can press 0 again. So yes, we treat that as a valid transition.
Thus the transition matrix T (10x10) where T[i][j]=1 if j is adjacent to i (including i itself). We need to compute number of sequences of length N (digits) where each consecutive pair (including staying) satisfies adjacency+self.
Thus count = sum over all start digits (10 possibilities) of number of walks of length N-1? Actually a sequence of length N corresponds to a start digit and then N-1 transitions. So total number = sum_{i,j} ( (T^(N-1))[i][j] )? Wait: For each start digit i, number of sequences of length N is number of walks of length N-1 starting at i: sum_{j} ( (T^(N-1))[i][j] ). So total = sum over i of sum over j ( (T^(N-1))[i][j] ) = sum of all entries of T^(N-1). If N=0? Constraint includes 0 ≤ N ≤ 10000. N=0 means a number of length 0? Possibly empty string? Usually length 0 might be considered 1 (empty number). But check problem statement: "numbers of length N", if N=0 might be 0-length number, maybe count = 1? But we need to deduce from examples: They didn't give N=0. However, we must handle it. Probably for N=0, answer is 1 (empty sequence). Or maybe they consider no numbers? But typical combinatorial count: sequences of length 0: 1. But let's check constraints: 0 ≤ N ≤ 10000. So handle N=0. We'll need to see if test expects 1 for N=0. Possibly yes. Could also be 0? But I'd assume 1. We'll consider later.
But sample: N=1 => output 10. For N=1, we have sequences of length 1: any digit (0-9) = 10. That matches: if we compute sum of all entries of T^(0) = identity matrix, sum = 10. So formula: total(N) = sum entries of T^(N-1) for N>=1, and for N=0, maybe 1? Or maybe they treat N=0 as 0? But sample only N=1,2. For N=2, output 36. Let's test with our adjacency including self-loops.
Compute number of length 2 sequences: For each start digit i, count of possible second digits (adjacent including self). Sum over i of out-degree (including self). So total = sum_{i} (degree including self). Compute from our graph:
- 1: neighbors: itself? +2,4 => set: {1,2,4} => 3
- 2: {2,1,3,5} => 4
- 3: {3,2,6} => 3
- 4: {4,1,5,7} => 4
- 5: {5,2,4,6,8} => 5
- 6: {6,3,5,9} => 4
- 7: {7,4,8} => 3
- 8: {8,5,7,9,0} => 5
- 9: {9,6,8} => 3
- 0: {0,8} => 2? Wait, self-loop allowed, so includes 0 itself, and neighbor 8 => set {0,8} => 2.
Sum: 3+4+3+4+5+4+3+5+3+2 = let's calculate: 3+4=7, +3=10, +4=14, +5=19, +4=23, +3=26, +5=31, +3=34, +2=36. Yes, matches sample output 36. So our adjacency and self-loop inclusion is correct.
Thus problem reduces to: For each N (up to 10000, t up to 100), compute total number of sequences of length N over 10 digits with allowed transitions given by graph with self-loops, modulo M = 10^9+9.
We need to compute for many test cases with N up to 10,000. Could precompute DP up to 10000 using iterative DP O(N * 10) = 100k, times t=100 => 10 million, which is fine. But we also have to answer multiple queries, so we can precompute answers for all N up to max N among test cases. Since max N=10000, we can do DP.
DP approach: Let f[i][digit] = number of sequences of length i ending at digit d. Then total for length i = sum_d f[i][d].
Initialize for i=1: f[1][d] = 1 for all d (10 digits). Or if we treat length 0 as 1 empty? We'll handle separately.
Recurrence: f[i+1][d] = sum over neighbors n of d (including self) f[i][n]. Since transitions are undirected? But careful: The condition is that you can press a digit that is adjacent to current digit. If you are at digit x and press y, then y must be adjacent to x. So in forward DP, when building sequence, from previous digit p, next digit c must be adjacent to p. So f[i+1][c] = sum_{p such that c adjacent to p} f[i][p]. Since adjacency is symmetric? Our graph is undirected? Yes, all adjacencies are symmetric (if p adjacent to c then c adjacent to p). But self loops are symmetric obviously. So we can compute.
Alternatively we can use transition matrix T where T[p][c] = 1 if c adjacent to p (including p=c). Then f[i] is a row vector? Actually if we have column vector v_i where v_i[d] = number of sequences length i ending at d, then v_{i+1} = T^T * v_i? Wait careful: The transition: number of sequences of length i+1 ending at c = sum_p (number of sequences of length i ending at p) * (allowed transition from p to c). That is v_{i+1}[c] = sum_p v_i[p] * A[p][c] where A[p][c] = 1 if allowed. So v_{i+1} = v_i * A? Actually if v_i is row vector (1x10), then v_{i+1} = v_i * A (since row vector times matrix gives row vector: (v_i * A)[c] = sum_p v_i[p] * A[p][c]). So A is transition matrix from state p to c. Starting v_1 = [1,1,...,1] (since for length 1, any digit is a sequence). Then total(N) = sum(v_N) = v_N * ones vector.
Alternatively we can compute total(N) = v_1 * A^(N-1) * ones.
But DP is simpler: for i from 1 to maxN-1, compute next array.
Complexity O(maxN * states * avg_degree) = 10000 * 10 * avg~4 = 400k, trivial.
But we need to consider modulo 1e9+9.
We also need to handle N=0. Probably answer 1 (empty sequence). Could also be 0? But typical count for length 0 is 1. We'll confirm: If N=0, they ask "numbers of length N", length zero numbers? Possibly not defined but constraints include 0. We'll check typical problem from HackerEarth or similar: "Mobile numeric keypad" problem often asks count of possible numbers of length N given that you can only press keys that are adjacent (including diagonals sometimes). Usually they define for N>0. But here N can be 0. Let's see sample: not include. I'll assume answer for N=0 is 1.
But we must be careful: The DP approach: total[0] = 1 (empty). total[1] = 10.
We'll compute.
Thus solution: Precompute answer array up to 10000.
However, note: N up to 10000 but t up to 100, so DP fine. But we might need to answer for many test cases, but still okay.
But wait, could N be up to 10^9? The constraints say 0 ≤ N ≤ 10000. So fine.
Thus we can just precompute.
But there might be nuance: The keypad might have different adjacency: Some problems consider diagonal moves as well. But here only left, right, up, bottom. So we defined correctly.
Edge cases: N=0, answer=1.
Also note that modulo is 10^9+9, not 10^9+7. So use mod = 1000000009.
Thus we can implement.
But let's double-check adjacency for digit 0: Does 0 also neighbor something else like maybe 5? In some layouts, 0 is below 8, but some keypads have 0 in the middle of bottom row with 1 above? Actually typical telephone keypad:
1 2 3
4 5 6
7 8 9
So 0 is below 8. So only neighbor is 8. But if we consider also that * and # are not allowed, so only neighbor digit is 8. However, some problems like "Mobile numeric keypad" on GeeksforGeeks often treat 0 as neighbor to 8 only. So matches.
Now check self-loop: For 0, self-loop allowed, so from 0 you can press 0 again.
Thus DP.
But is there any possibility that the keypad's "0" is adjacent to "5"? In some old mobile keypads, 0 is below 8? Actually many numeric keypads on keyboards (like numpad) have 0 below 1,2,3? But that's a different layout. The problem says "numeric keypad of mobile". So typical mobile phone keypad is as above.
Thus we are good.
Now let's write solution.
We'll define adjacency lists as:
adj[0] = [0,8]
adj[1] = [1,2,4]
adj[2] = [2,1,3,5]
adj[3] = [3,2,6]
adj[4] = [4,1,5,7]
adj[5] = [5,2,4,6,8]
adj[6] = [6,3,5,9]
adj[7] = [7,4,8]
adj[8] = [8,5,7,9,0]
adj[9] = [9,6,8]
Alternatively we can compute from grid programmatically but it's fine.
DP: Initialize cur[0..9]=1 for length 1. Then for step from 2 to maxN:
nxt[d] = sum over p in adj[d] (cur[p]) mod M.
But careful: The transition from previous p to next d is allowed if d is adjacent to p, i.e., if d in adj[p] (or p in adj[d] since symmetric). So for each next digit d, we sum cur[p] for all p such that d is in adj[p]. Since we have adjacency lists for each p, it's easier to compute nxt by iterating over p and adding cur[p] to all its neighbors (including itself). That is: initialize nxt[d]=0 for all d. For each p from 0 to 9, for each neighbor n in adj[p], do nxt[n] += cur[p]. That ensures nxt[n] = sum_{p: n in adj[p]} cur[p] which is exactly the recurrence. Since adjacency lists include self-loop, we'll get contributions to self.
Alternatively we can precompute reverse adjacency: for each d, list of p that can go to d. Since graph undirected, reverse adjacency is same as forward: p can go to d if they are adjacent or same. So reverse list for d is same as adj[d] (since adjacency is symmetric). But careful: if p can go to d, that means d is adjacent to p. Since adjacency is symmetric, p in adj[d] iff d in adj[p]. So we can just use adj[d] as list of p that can transition to d. That is correct because if d is adjacent to p, then p is adjacent to d, so p ∈ adj[d]. So for each d, nxt[d] = sum_{p ∈ adj[d]} cur[p]. Because adj[d] contains all nodes p such that p and d are adjacent (including p = d). So we can do that.
Thus algorithm:
mod = 1000000009
maxN = max(all inputs)
ans[0] = 1
if maxN >= 1:
cur = [1]*10
ans[1] = 10
for len in range(2, maxN+1):
nxt = [0]*10
for d in range(10):
# sum over neighbors p of d
for p in adj[d]:
nxt[d] = (nxt[d] + cur[p]) % mod
cur = nxt
ans[len] = sum(cur) % mod
Then for each N, print ans[N].
Let's test manually: len=2:
cur from len=1: all ones.
For each d:
- d=0: adj[0]=[0,8] => nxt[0] = cur[0]+cur[8] = 1+1=2
- d=1: adj[1]=[1,2,4] => 1+1+1=3
- d=2: [2,1,3,5] => 1+1+1+1=4
- d=3: [3,2,6] => 1+1+1=3
- d=4: [4,1,5,7] => 1+1+1+1=4
- d=5: [5,2,4,6,8] => 1+1+1+1+1=5
- d=6: [6,3,5,9] => 1+1+1+1=4
- d=7: [7,4,8] => 1+1+1=3
- d=8: [8,5,7,9,0] => 1+1+1+1+1=5
- d=9: [9,6,8] => 1+1+1=3
sum = 2+3+4+3+4+5+4+3+5+3 = 36. Good.
Now for len=3 we can compute if needed.
Thus solution is simple.
But note: N can be 0, ans[0]=1.
Now, is there any nuance about leading zeros? They said "numbers" but they gave example "000" and "080" for length 3, so leading zeros are allowed. So we treat all sequences equally.
Thus final answer.
But we must consider time: precomputing up to 10,000 with loops over 10 and neighbors (max 5) is fast.
Alternatively we could use matrix exponentiation for larger N, but not needed.
Thus implement.
Now careful: Input reading: first line T, then T lines each N. Possibly there are multiple test cases, so we need to read all Ns, find maxN, compute up to maxN, then output answers.
Potential pitfalls: N can be 0 and 1. For N=0, ans[0]=1. For N=1, ans[1]=10.
Now let's think about memory: we only need cur and ans array of size maxN+1 (maybe 10001) store answers, that's fine.
Now implement.
But check modulo: 10^9+9 = 1000000009.
Now I'm going to write code.
But before finalize, verify adjacency for 0: Our adjacency list includes [0,8]. But note: In transition from p to d, we consider p in adj[d]. For d=0, adj[0] = [0,8]. That means if we are at previous digit 0 or 8, we can go to 0. Is that correct? From 8 you can go to 0 (since 0 is adjacent to 8). Yes. From 0 you can stay on 0. So correct.
But also, could 0 be adjacent to something else like 5? In some older mobile phones, keypad is 3x3 + 0 below? Actually many feature phones have keypad:
1 2 3
4 5 6
7 8 9
0
with 0 below the 8? Or sometimes 0 below 5? Actually many phones have keypad like:
1 2 3
4 5 6
7 8 9
But some have no * and #, just 0 at bottom center? But they said cannot press * and # keys, so presumably they exist but not allowed. So our mapping is standard.
Thus it's fine.
Now we should think about possibility of 'adjacent' including diagonals? They said left, right, up, bottom, so only 4 directions, not diagonals. So correct.
Now code.
Implementation details:
We can hardcode adjacency as list of lists:
adj = [
[0,8], # 0
[1,2,4], # 1
[2,1,3,5], # 2
[3,2,6], # 3
[4,1,5,7], # 4
[5,2,4,6,8], # 5
[6,3,5,9], # 6
[7,4,8], # 7
[8,5,7,9,0], # 8
[9,6,8] # 9
]
Alternatively we can compute from grid positions. But this is fine.
Now DP loop:
if maxN >= 1:
cur = [1]*10
ans[1] = 10
for length in range(2, maxN+1):
nxt = [0]*10
for d in range(10):
s = 0
for p in adj[d]:
s = (s + cur[p]) % mod
nxt[d] = s
cur = nxt
ans[length] = sum(cur) % mod
Edge case: if maxN == 0, then ans[0] = 1 only.
Now handle multiple test cases: read all N into list, compute maxN, compute answers up to that maxN, then output each.
Complexity: O(maxN * (10 * avg_deg)) ~ 10000 * 50 = 500k operations, trivial.
Now we need to ensure to use modulo after each addition.
Potential improvement: Precompute for each digit the list of neighbors. Already done.
Now test with given sample:
T=2, N=1,2 -> maxN=2.
Compute: ans[1]=10, ans[2]=36, output.
Ok.
Now consider N=0 (if provided): ans[0]=1.
Thus solution done.
But wait: We need to be careful: The number of sequences of length N: They said "numbers of length N are possible". They gave example "000 & 080 are possible for length 3". So they consider sequences of digits of length N, not necessarily numbers without leading zero? But they gave 000, which is basically number 0 with leading zeros. So it's okay.
Thus final answer.
Now, thinking about potential pitfalls: Could the keypad be different? For instance, some keypads have 0 below 5? In many older phones, the layout is:
1 2 3
4 5 6
7 8 9
I think it's standard. So we are good.
Also, note: In some interpretations of "adjacent", pressing the same key again might not be considered adjacent, but they explicitly allowed it. So our self-loops are included.
Now we should confirm that for N=2 our DP yields 36, matches sample.
Now, check N=3 manually perhaps to see if our DP yields correct known values from similar problems? For example, typical "Mobile numeric keypad" problem (where you can only move to adjacent keys but cannot press same key again) yields different numbers. But with self-loop it's different. But we don't have sample for N=3, but it's fine.
Let's compute quickly: Using DP as above.
cur for len=2 we computed: [2,3,4,3,4,5,4,3,5,3] (these are counts ending at each digit). Then len=3: nxt for each d:
- d0: adj[0]=[0,8] => cur[0]+cur[8] = 2+5=7
- d1: adj[1]=[1,2,4] => 3+4+4=11? Actually cur[1]=3, cur[2]=4, cur[4]=4 => sum=11
- d2: [2,1,3,5] => 4+3+3+5=15? cur[2]=4, cur[1]=3, cur[3]=3, cur[5]=5 => 15
- d3: [3,2,6] => 3+4+4=11
- d4: [4,1,5,7] => 4+3+5+3=15
- d5: [5,2,4,6,8] => 5+4+4+4+5=22
- d6: [6,3,5,9] => 4+3+5+3=15
- d7: [7,4,8] => 3+4+5=12
- d8: [8,5,7,9,0] => 5+5+3+3+2? Wait cur[8]=5, cur[5]=5, cur[7]=3, cur[9]=3, cur[0]=2 => sum=5+5+3+3+2=18
- d9: [9,6,8] => 3+4+5=12? cur[9]=3, cur[6]=4, cur[8]=5 => 12
Sum total = 7+11+15+11+15+22+15+12+18+12 = add: 7+11=18, +15=33, +11=44, +15=59, +22=81, +15=96, +12=108, +18=126, +12=138. So total length 3 = 138. Is this correct? Possibly.
Now check quickly if any known sequences: Not needed.
Now, we should ensure that we treat N=0 correctly: ans[0]=1.
Now code accordingly.
But there is also possibility that N is large (10,000) and we need to be careful about recursion depth? We'll use loops.
Now, final solution: Read T, list Ns, compute maxN, precompute answers up to maxN, then output.
But we might also consider direct formula using matrix exponentiation? Not needed.
Now, the problem might be from some contest where they expect efficient matrix exponentiation because N up to 10^9? But constraints say 0 ≤ N ≤ 10000, so DP is enough.
However, we should double-check constraints: The text says "Constraints: 1 ≤ t ≤ 100, 0 ≤ N ≤ 10000, MOD: 10^9 + 9". That's what we have. So DP is fine.
Thus final answer.
But I'm going to consider a more optimized way: We could compute using linear recurrence maybe but not needed.
Now, we need to write solution in Python (assume language is Python). We'll produce final answer.
But before final, test with sample.
Now code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
Ns = list(map(int, data[1:1+t]))
maxN = max(Ns) if Ns else 0
mod = 1000000009
ans = [0]*(maxN+1)
ans[0] = 1
if maxN >= 1:
cur = [1]*10
ans[1] = 10
# precompute adjacency list
adj = [
[0,8], #0
[1,2,4], #1
[2,1,3,5], #2
[3,2,6], #3
[4,1,5,7], #4
[5,2,4,6,8], #5
[6,3,5,9], #6
[7,4,8], #7
[8,5,7,9,0], #8
[9,6,8] #9
]
for length in range(2, maxN+1):
nxt = [0]*10
for d in range(10):
s = 0
for p in adj[d]:
s = (s + cur[p]) % mod
nxt[d] = s
cur = nxt
total = sum(cur) % mod
ans[length] = total
out = '\n'.join(str(ans[n]) for n in Ns)
sys.stdout.write(out)
Now, check for N=0: ans[0]=1.
But wait: Should N=0 output 0? Let's think: "numbers of length N" for N=0, maybe they consider no number, count 0. But typical combinatorial counting of strings of length 0 is 1 (empty string). However, sometimes problems treat length 0 as invalid and output 0. But given they explicitly include 0 in constraints, they likely expect a result. In many DP problems for "numbers of length N", N=0 yields 1. For example, similar problems like "count numbers of length N using keypad" often start from N=1. But if N=0 allowed, they might output 1. Let's search memory: I recall a problem "Mobile numeric keypad" on GeeksforGeeks, they consider N from 1 upwards. But here they have 0 ≤ N ≤ 10000. I'll examine the sample: They didn't show 0. Possibly they want 1. But we can check logically: For N=0, there is exactly one way: press nothing. However, does it constitute a "number"? Possibly not, but they included it for completeness.
But there is also the possibility that they consider number of length 0 as 0 because you can't form any number. Yet the problem statement: "Given a number N, DJ boy has to tell how many numbers of length N are possible." If N=0, length 0 numbers? Usually we don't talk about numbers of length 0, but if forced, could be empty string. In many programming problems, when N=0, they define count as 0 or 1. I've seen some problems where they define count as 0 for N=0 because there is no number of length 0. But sometimes they define as 1 for base case of DP. To decide, think about the recurrence: For N=1, we have 10 numbers. For N=2, we have 36. Could there be a recurrence that includes N=0? For instance, total(N) = sum over i: ??? Not sure.
But consider transition: The number of sequences of length N can be expressed as vector * matrix. If we define v0 as empty? Not needed.
But we can check if any common sense: For typical "keypad sequences" problems, if they ask for N-digit numbers (leading zeros allowed), N=0 is sometimes considered to have 1 possibility (the empty number) especially when N is allowed to be 0. I've seen problems like "Count the number of n-digit numbers formed by digits 1-9 such that ..." and they sometimes include 0 and output 1. But not sure.
Let's examine adjacency: For N=1, answer 10. For N=0, answer should be 1 because there is exactly one way: no digits. If they wanted 0, they'd probably exclude N=0. But they explicitly include it, so they expect a non-zero answer. I'd go with 1.
But we can also reason by formula: total(N) = sum of entries of T^(N-1) * initial? If we define total(1)=10 = sum( T^0 * v1 ), v1 = ones. For N=0, we might think total(0) = 1 (empty sequence). In DP, we can initialize dp[0][digit] = 1? That would mean sequences of length 0 ending at each digit? Typically we don't. So ans[0] = 1 is fine.
Thus I'll keep ans[0]=1.
Now potential pitfalls: Precomputation for maxN=0 (if all Ns are 0). Then we skip DP, ans[0]=1, output accordingly.
Now, note: Our adjacency list for each digit includes the digit itself. That ensures self-loop.
Double-check adjacency for 2: We have [2,1,3,5] correct? Actually, 2's neighbors: self, left:1, right:3, down:5. So yes.
For 5: self, up:2, left:4, right:6, down:8. So [5,2,4,6,8]. Good.
Now, one more nuance: Could the keypad have 0 adjacent to something else like maybe 7 and 9? In some interpretations, the * and # are considered keys that are not allowed, but they are adjacent to 0. But they are not digits, so they are not allowed as next press. So they don't count. So no.
Thus final solution.
Now, we need to present solution explanation in final output. The problem is to provide code.
But also, we need to consider time constraints: For T up to 100, precomputing up to 10000 is fine.
Now, I'll write final code.
But before final, consider if we need to handle big test cases with same N: we store answers.
Now, possible alternative: Could we compute using matrix exponentiation for faster? Not required.
Now, let's test manually for N=2: get 36.
Now check N=4 just for fun: I could compute quickly with DP to see if numbers are consistent? Not needed.
Now, ensure that we are using modulo correctly: After computing sum for total, mod.
Now, answer.
Now I'd like to think about whether the graph is undirected or directed? Since adjacency is symmetric, it's undirected. So both ways work.
Now, in recurrence we used for each d: sum over p in adj[d] cur[p]. That's correct because p in adj[d] means p and d are adjacent (or same). So from p you can go to d.
Now we can precompute adjacency lists as a list of lists. But note: For d=0, adj[0] = [0,8]. So we are summing cur[0] and cur[8]. That's fine.
Now let's consider if there is any case where adjacency is not symmetric? For self-loops it's symmetric, and for edges they are symmetric because if p is left of d then d is right of p, so both directions exist. So ok.
Now, what about 0 and 8? 8 is up of 0, so symmetric.
Thus good.
Now code.
Edge Cases:
- N=0 -> output 1.
- N=1 -> 10.
- N=2 -> 36.
All good.
Now, I'll include reading and writing.
One more thing: The problem says "MOD: 10^9 + 9". So use that.
Now, final solution in Python.
Will we face performance issues? Python loops: 10000 * 10 * up to 5 = 500k operations, negligible.
Now, finalize.
But I'll also consider that we can precompute the transition matrix and then use exponentiation for fun but DP simpler.
Now produce final answer with explanation.
Given the interactive nature (the system wants answer only), we'll provide code.
Now, let's double-check the adjacency for digit 0: In some mobile keypads, 0 is at the bottom middle, but also adjacent to * and #. But those are not digits. Also sometimes 0 is adjacent to 5? No, because 5 is above 8, not 0. 0 is below 8. So it's only adjacent to 8. However, if we consider "adjacent" includes diagonal, then 0 might be adjacent to 7 and 9, but they said only left, right, up, bottom. So not.
Thus adjacency is fine.
Now also consider digit 1: neighbors: 2 (right), 4 (down). Also maybe 5? No diagonal.
Thus we have correct.
Now we can implement.
But there is also possibility that the keypad orientation is like in some old phones: 0 below 5, but that would be a different layout. I'd better confirm typical mobile numeric keypad: Actually many mobile phones have the keypad layout:
1 2 3
4 5 6
7 8 9
So 0 below 8. In some other designs, 0 is placed below 5? Actually the standard telephone keypad (ITU-T E.161) is:
1 2 3
4 5 6
7 8 9
So 0 is below 8. So correct.
Now proceed.
Now I want to ensure that the DP uses modulo after each sum to avoid overflow, though Python ints are big.
Now, to output for each test case.
Now I'll write final answer.
But maybe we need to handle input where T lines after first line contain N. Possibly there might be extra spaces or lines. Using sys.stdin.read() is robust.
Now, I'll produce final solution.
Now include in final: Explanation then code.
Since it's a coding problem, final answer should be code.
Now, the instruction: "Your answer should be the final solution with code." I'll provide Python code.
Now, I'll include comments.
Thus final. ```python
import sys
MOD = 1000000009
adjacency list including self-loops
adj = [
[0, 8], # 0
[1, 2, 4], # 1
[2, 1, 3, 5], # 2
[3, 2, 6], # 3
[4, 1, 5, 7], # 4
[5, 2, 4, 6, 8], # 5
[6, 3, 5, 9], # 6
[7, 4, 8], # 7
[8, 5, 7, 9, 0], # 8
[9, 6, 8] # 9
]
def precompute(max_n):
"""Return list ans where ans[n] = count for length n (0 <= n <= max_n)."""
ans = [0] * (max_n + 1)
ans[0] = 1 # empty sequence
if max_n == 0:
return ans
# length 1
cur = [1] * 10 # f[1][d] = 1 for all digits
ans[1] = 10
for length in range(2, max_n + 1):
nxt = [0] * 10
for d in range(10):
total = 0
for p in adj[d]: # p can move to d
total = (total + cur[p]) % MOD
nxt[d] = total
cur = nxt
ans[length] = sum(cur) % MOD
return ans
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
Ns = list(map(int, data[1:1 + t]))
max_n = max(Ns) if Ns else 0
ans = precompute(max_n)
out = '\n'.join(str(ans[n]) for n in Ns)
sys.stdout.write(out)
if name == "main":
solve()