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)))))
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
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
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fef7c41bc-9ec8-4b0d-8392-fba4e5be0a35%2Fb0c1f230-2078-40ed-a1d8-a2d2109b92fe%2Foruxagh_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education