0% found this document useful (0 votes)
72 views

Codevita Executed Program

The document describes a problem where a class has N books and N students. Each week, the books are exchanged so that each student receives a different book than the one they had. The task is to calculate the number of possible exchanges modulo 1000000007. The number of possible exchanges when there are 4 books and students is 9. The input is N, the number of books/students, and the output is the number of possible exchanges modulo 1000000007.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Codevita Executed Program

The document describes a problem where a class has N books and N students. Each week, the books are exchanged so that each student receives a different book than the one they had. The task is to calculate the number of possible exchanges modulo 1000000007. The number of possible exchanges when there are 4 books and students is 9. The input is N, the number of books/students, and the output is the number of possible exchanges modulo 1000000007.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Codevita Program

Distribute Books
Problem Description:
For enhancing the book reading, school distributed story books to students
as part of the Children’s day celebrations.

To increase the reading habit, the class teacher decided to exchange the
books every weeks so that everyone will have a different book to read. She
wants to know how many possible exchanges are possible.

If they have 4 books and students, the possible exchanges are 9. Bi is the book
of i-th student and after the exchange he should get a different book, other
than Bi.

B1 B2 B3 B4 - first state, before exchange of the books

B2 B1 B4 B3

B2 B3 B4 B1

B2 B4 B1 B3

B3 B1 B4 B2

B3 B4 B1 B2
B3 B4 B2 B1

B4 B1 B2 B3

B4 B3 B1 B2

B4 B3 B2 B1

Find the number of possible exchanges, if the books are exchanged so that
every student will receive a different book.

Constraints:
1<= N <= 1000000

Input Format:
Input contains one line with N, indicates the number of books and number
of students.

Output:
Output the answer modulo 1000000007.

Test Case:

Explanation:
Example 1

Input
4

Output:

9
Executed Program:
Language : Python

import sys
n = int(sys.stdin.readline())
def fun(n):
MOD = 10**9+7
f=s=1
for i in range(n+1):
f = (f*i+s) % MOD
s = -s
return f%MOD

val=fun(n)
print(val)
Flow chart

You might also like