Problem Description
Given a test with n types of questions, where for each type you have a certain count of questions and each question of that type gives specific marks, determine the number of ways to score exactly a given target points. Questions of the same type are indistinguishable. Return the answer modulo 10^9 + 7.
Key Insights
- This is a bounded knapsack (or coin change) style problem with limited supply for each type.
- Use dynamic programming where dp[i] represents the number of ways to reach exactly i points.
- For each question type, iterate over dp and update counts by considering solving 0, 1, ..., up to count questions of that type.
- The answer is computed under modulo arithmetic due to the large size of possible outcomes.
Space and Time Complexity
Time Complexity: O(n * target * max_count)
Space Complexity: O(target)
Solution
We solve the problem using a dynamic programming approach. We initialize a dp array of size (target + 1) with dp[0] = 1 (base case: one way to accumulate 0 points). For each question type represented by [count, marks], update the dp array in reverse order (to avoid using updated states prematurely) by considering how many questions of that type we take (from 1 up to count) without exceeding the target. Each choice adds to the ways to achieve the current total score. This is effectively a bounded knapsack problem where we count the combinations rather than optimize a value. Finally, the computed dp[target] (modulo 10^9 + 7) gives the number of ways to score exactly the target points.