Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python. The time complexity has to be as less as possible (nlogn or n at best, no n2) and runtime should be under 1 second. Apply dynamic programming. Do not use recursion. Make sure ALL test cases return expected outputs. Output Format For each query, output one line containing an integer Dn. The number must be output mod (109 + 7). Sample Input 0: 5 0 1 2 3 4 Sample Output 0: 1 0 1 2 9 Edge Case: (to test against runtime and timeout error) 300000 (number of queries) 300000 300000 300000 300000 ... and so on until there are 300,000 of them  The actual code: def solve(n):     MOD = 1000000007     # compute and return answer here        q = int(input().rstrip()) outs = [] for i in range(q):     n = int(input().rstrip())     outs.append(solve(n)) print("{}".format("\n".join(list(map(str,outs)))))

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python. The time complexity has to be as less as possible (nlogn or n at best, no n2) and runtime should be under 1 second. Apply dynamic programming. Do not use recursion. Make sure ALL test cases return expected outputs.

Output Format
For each query, output one line containing an integer Dn. The number must be output mod (109 + 7).

Sample Input 0:
5
0
1
2
3
4

Sample Output 0:
1
0
1
2
9

Edge Case: (to test against runtime and timeout error)
300000 (number of queries)
300000
300000
300000
300000
...
and so on until there are 300,000 of them 

The actual code:

def solve(n):
    MOD = 1000000007
    # compute and return answer here
    
 
q = int(input().rstrip())
outs = []
for i in range(q):
    n = int(input().rstrip())
    outs.append(solve(n))
print("{}".format("\n".join(list(map(str,outs)))))
A derangement of N objects is a permutation wherein none of the N objects are in the correct position. For
example, for the set 1, 2, 3, the arrangements are
• 1, 2, 3
• 1,3,2
• 2,1,3
3, 2, 1
Since in the first 2 permutations, 1 is in the correct spot. In the 1st and 4th permutation, 2 is in the right spot.
In the 1st and 3rd permutation, 3 is in the correct spot. Therefore the derangements are
• 2, 3, 1
3,1,2
The formula for the number of derangements for N objects is defined as:
Do = 1
D₁ = 0
Dn = (Dn−1 + Dn-2) · (n − 1) for n ≥ 2
You have to answer Q queries. Each query consists of an integer n. You must compute for D. This number
may be very large, so output it mod 10⁹ +7.
Input Format
Input starts with one line containing a single integer Q, indicating the number of queries.
Qlines follow, each containing a single integer n.
Constraints
1≤Q≤3.105
0 ≤ n ≤ 3.105
Transcribed Image Text:A derangement of N objects is a permutation wherein none of the N objects are in the correct position. For example, for the set 1, 2, 3, the arrangements are • 1, 2, 3 • 1,3,2 • 2,1,3 3, 2, 1 Since in the first 2 permutations, 1 is in the correct spot. In the 1st and 4th permutation, 2 is in the right spot. In the 1st and 3rd permutation, 3 is in the correct spot. Therefore the derangements are • 2, 3, 1 3,1,2 The formula for the number of derangements for N objects is defined as: Do = 1 D₁ = 0 Dn = (Dn−1 + Dn-2) · (n − 1) for n ≥ 2 You have to answer Q queries. Each query consists of an integer n. You must compute for D. This number may be very large, so output it mod 10⁹ +7. Input Format Input starts with one line containing a single integer Q, indicating the number of queries. Qlines follow, each containing a single integer n. Constraints 1≤Q≤3.105 0 ≤ n ≤ 3.105
Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education