Adding 1, 2, 3 (9)
InterviewTime limit1sMemory limit512 MB
Count ordered compositions of n into parts 1, 2, and 3 that use at most m terms, and output each count modulo 1,000,000,009.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Combinatorics, Math, Prefix sum
- Solved
- No attempts yet
Problem
There are 7 ways to express the integer 4 as a sum of 1, 2, and 3. A sum must use at least one number.
- 1+1+1+1
- 1+1+2
- 1+2+1
- 2+1+1
- 2+2
- 1+3
- 3+1
Given integers n and m, write a program that finds the number of ways to express n as a sum of 1, 2, and 3. The number of terms used must be at most m.
Input
The first line gives the number of test cases T. Each test case occupies one line and contains the integers n and m. n is a positive integer no greater than 1,000. m is also a positive integer no greater than n.
Output
For each test case, print the number of ways to express n as a sum of 1, 2, and 3, modulo 1,000,000,009. The number of terms used must be at most m.