Objective In this challenge, we're going to use loops to help us do some simple math. Task Given an integer, , print its first multiples. Each multiple (where ) should be printed on a new line in the form: N x i = result . Input Format A single integer, . Constraints Output Format Print lines of output; each line (where ) contains the of in the form: N x i = result . Sample Input 2 Sample Output 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 Explanation: Here, we just need to use for loops to achieve the result Solution : import java.io.* ; import java.math.* ; import java.security.* ; import java.text.* ; import java.util.* ; import java.util.concurrent.* ; import java.util.regex.* ; public class Solution { public static void main ( String [] args ) throws IOException { BufferedReader bufferedReader = new BufferedReader ( new InputStreamReader ( Syste
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
Example 1
Input
4
Output
9
Code Logic:
- Input No.of.Books
- function for find out dearrangement
- Base condition n==1 book means return 0
- n==0 means return 1
- Declare fib list of (n+1) size
- fib[1]=0
- fib[2]=1
- Using dearrangement formula fib[i]=(i-1)*(fib[i-1]+fib[i-2])%100000007 update the list.
- Print the last element in list
Program:
Comments
Post a Comment