Skip to content

Commit ac1ae2a

Browse files
singh-shreya6rishabhgarg25699
authored andcommitted
Added Partial Dearrangements (jainaman224#1403)
1 parent 9edd01c commit ac1ae2a

5 files changed

+316
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
PARTIAL DEARRANGEMENTS
3+
4+
A partial dearrangement is a dearrangement where some points are
5+
fixed. That is, given a number n and a number k, we need to find
6+
count of all such dearrangements of n numbers, where k numbers are
7+
fixed in their position.
8+
*/
9+
10+
#include<stdio.h>
11+
12+
int mod = 1000000007;
13+
14+
int nCr(int n, int r, int mod) {
15+
if (n < r) {
16+
return -1;
17+
}
18+
// We create a pascal triangle.
19+
int Pascal[r + 1];
20+
Pascal[0] = 1;
21+
for (int i = 1; i <= r; i++) {
22+
Pascal[i] = 0;
23+
}
24+
25+
// We use the known formula nCr = (n-1)C(r) + (n-1)C(r-1) for computing the values.
26+
for (int i = 1; i <= n; i++) {
27+
int k = (i < r) ? (i) : (r);
28+
// we know, nCr = nC(n-r). Thus, at any point we only need min
29+
for (int j = k; j > 0; j--) {
30+
// of the two, so as to improve our computation time.
31+
Pascal[j] = (Pascal[j] + Pascal[j - 1]) % mod;
32+
}
33+
}
34+
return Pascal[r];
35+
}
36+
37+
int count(int n, int k) {
38+
if (k == 0) {
39+
if (n == 0) {
40+
return 1;
41+
}
42+
if (n == 1) {
43+
return 0;
44+
}
45+
return (n - 1) * (count(n - 1, 0) + count(n - 2, 0));
46+
}
47+
return nCr(n, k, mod) * count(n - k, 0);
48+
}
49+
50+
int main() {
51+
int number;
52+
scanf("%d", &number);
53+
int k;
54+
scanf("%d", &k);
55+
int dearrangements = count(number, k);
56+
printf("The number of partial dearrangements is %d", dearrangements);
57+
}
58+
59+
/*
60+
INPUT : n = 6
61+
k = 3
62+
OUTPUT: The number of partial dearrangements is 40
63+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
PARTIAL DEARRANGEMENTS
3+
4+
A partial dearrangement is a dearrangement where some points are
5+
fixed. That is, given a number n and a number k, we need to find
6+
count of all such dearrangements of n numbers, where k numbers are
7+
fixed in their position.
8+
*/
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
13+
int mod = 1000000007;
14+
15+
int nCr(int n, int r, int mod) {
16+
if (n < r) {
17+
return -1;
18+
}
19+
// We create a pascal triangle.
20+
int Pascal[r+1];
21+
Pascal[0] = 1;
22+
for (int i = 1; i <= r; i++) {
23+
Pascal[i] = 0;
24+
}
25+
26+
// We use the known formula nCr = (n-1)C(r) + (n-1)C(r-1) for computing the values.
27+
for (int i = 1; i <= n; i++) {
28+
int k = (i < r) ? (i) : (r);
29+
// we know, nCr = nC(n-r). Thus, at any point we only need min
30+
for (int j = k; j > 0; j--) {
31+
// of the two, so as to improve our computation time.
32+
Pascal[j] = (Pascal[j] + Pascal[j - 1]) % mod;
33+
}
34+
}
35+
return Pascal[r];
36+
}
37+
38+
int count(int n, int k) {
39+
if (k == 0) {
40+
if (n == 0) {
41+
return 1;
42+
}
43+
if (n == 1) {
44+
return 0;
45+
}
46+
return (n - 1) * (count(n - 1, 0) + count(n - 2, 0));
47+
}
48+
return nCr(n, k, mod) * count(n - k, 0);
49+
}
50+
51+
int main() {
52+
int number;
53+
cin >> number;
54+
int k;
55+
cin >> k;
56+
int dearrangements = count(number, k);
57+
cout << "The number of partial dearrangements is " << dearrangements;
58+
}
59+
60+
/*
61+
INPUT : n = 6
62+
k = 3
63+
OUTPUT: The number of partial dearrangements is 40
64+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
PARTIAL DEARRANGEMENTS
3+
4+
A partial dearrangement is a dearrangement where some points are
5+
fixed. That is, given a number n and a number k, we need to find
6+
count of all such dearrangements of n numbers, where k numbers are
7+
fixed in their position.
8+
*/
9+
10+
package main
11+
12+
import (
13+
"fmt"
14+
)
15+
16+
var mod int = 1000000007
17+
18+
func nCr(n int, r int, mod int) int {
19+
if (n < r) {
20+
return -1
21+
}
22+
// We create a pascal triangle.
23+
var Pascal []int
24+
Pascal = append(Pascal, 1)
25+
for i := 1; i <= r; i++ {
26+
Pascal = append(Pascal, 0)
27+
}
28+
29+
// We use the known formula nCr = (n-1)C(r) + (n-1)C(r-1) for computing the values.
30+
for i := 1; i <= n; i++ {
31+
var k int
32+
if i < r {
33+
k = i
34+
} else {
35+
k = r
36+
}
37+
38+
// we know, nCr = nC(n-r). Thus, at any point we only need min
39+
for j := k; j > 0; j-- {
40+
// of the two, so as to improve our computation time.
41+
Pascal[j] = (Pascal[j] + Pascal[j - 1]) % mod
42+
}
43+
}
44+
return Pascal[r]
45+
}
46+
47+
func count(n int, k int) int {
48+
if (k == 0) {
49+
if (n == 0) {
50+
return 1
51+
}
52+
if (n == 1) {
53+
return 0
54+
}
55+
return (n - 1) * (count(n - 1, 0) + count(n - 2, 0))
56+
}
57+
return nCr(n, k, mod) * count(n - k, 0)
58+
}
59+
60+
func main() {
61+
var number int
62+
fmt.Scan(&number)
63+
var k int
64+
fmt.Scan(&k);
65+
dearrangements := count(number, k)
66+
fmt.Print("The number of partial dearrangements is ", dearrangements)
67+
}
68+
69+
/*
70+
INPUT : n = 6
71+
k = 3
72+
OUTPUT: The number of partial dearrangements is 40
73+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
PARTIAL DEARRANGEMENTS
3+
4+
A partial dearrangement is a dearrangement where some points are
5+
fixed. That is, given a number n and a number k, we need to find
6+
count of all such dearrangements of n numbers, where k numbers are
7+
fixed in their position.
8+
*/
9+
10+
import java.util.Scanner;
11+
12+
class Partial_Dearrangement {
13+
14+
public static int mod = 1000000007;
15+
16+
public static int nCr(int n, int r, int mod) {
17+
if (n < r) {
18+
return -1;
19+
}
20+
// We create a pascal triangle.
21+
int Pascal[] = new int[r + 1];
22+
Pascal[0] = 1;
23+
for (int i = 1; i <= r; i++) {
24+
Pascal[i] = 0;
25+
}
26+
27+
// We use the known formula nCr = (n-1)C(r) + (n-1)C(r-1)
28+
// for computing the values.
29+
for (int i = 1; i <= n; i++) {
30+
int k = (i < r) ? (i) : (r);
31+
// we know, nCr = nC(n-r). Thus, at any point we only need min
32+
for (int j = k; j > 0; j--) {
33+
// of the two, so as to improve our computation time.
34+
Pascal[j] = (Pascal[j] + Pascal[j - 1]) % mod;
35+
}
36+
}
37+
return Pascal[r];
38+
}
39+
40+
public static int count(int n, int k) {
41+
if (k == 0) {
42+
if (n == 0) {
43+
return 1;
44+
}
45+
if (n == 1) {
46+
return 0;
47+
}
48+
return (n - 1) * (count(n - 1, 0) + count(n - 2, 0));
49+
}
50+
return nCr(n, k, mod) * count(n - k, 0);
51+
}
52+
53+
public static void main(String args[]) {
54+
int number;
55+
Scanner s = new Scanner(System.in);
56+
number = s.nextInt();
57+
int k;
58+
k = s.nextInt();
59+
int dearrangements = count(number, k);
60+
System.out.print("The number of partial dearrangements is " + dearrangements);
61+
}
62+
}
63+
64+
/*
65+
INPUT : n = 6
66+
k = 3
67+
OUTPUT: The number of partial dearrangements is 40
68+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
PARTIAL DEARRANGEMENTS
3+
4+
A partial dearrangement is a dearrangement where some points are
5+
fixed. That is, given a number n and a number k, we need to find
6+
count of all such dearrangements of n numbers, where k numbers are
7+
fixed in their position.
8+
'''
9+
10+
mod = 1000000007
11+
12+
def nCr(n, r, mod):
13+
if n < r:
14+
return -1
15+
# We create a pascal triangle.
16+
Pascal = []
17+
Pascal.append(1)
18+
for i in range(0, r):
19+
Pascal.append(0)
20+
# We use the known formula nCr = (n-1)C(r) + (n-1)C(r-1)
21+
# for computing the values.
22+
for i in range(0, n + 1):
23+
k = ((i) if (i < r) else (r))
24+
# We know, nCr = nC(n-r). Thus, at any point we only need min
25+
# of the two, so as to improve our computation time.
26+
for j in range(k, 0, -1):
27+
Pascal[j] = (Pascal[j] + Pascal[j - 1]) % mod
28+
return Pascal[r]
29+
30+
def count(n, k):
31+
if k == 0:
32+
if n == 0:
33+
return 1
34+
if n == 1:
35+
return 0
36+
return (n - 1) * (count(n - 1, 0) + count(n - 2, 0))
37+
return nCr(n, k, mod) * count(n - k, 0)
38+
39+
number = int(input())
40+
k = int(input())
41+
dearrangements = count(number, k)
42+
print("The number of partial dearrangements is", dearrangements)
43+
44+
'''
45+
INPUT : n = 6
46+
k = 3
47+
OUTPUT: The number of partial dearrangements is 40
48+
'''

0 commit comments

Comments
 (0)