forked from vidyavardhaka-college-of-engineering/tpec4-non-it-ece-a-assignment-2-and-3-Assignment-2-and-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudentprogram.c
53 lines (40 loc) · 1.25 KB
/
studentprogram.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
long long int assign(int n, int preference[][n]) {
int mx = 1 << n;
long long int dp[mx];
// Initialize all values of dp to 0
for (int i = 0; i < mx; i++) {
dp[i] = 0;
}
// Base case
dp[mx - 1] = 1;
for (int mask = mx - 2; mask >= 0; mask--) {
// Count the number of set bits in mask
int s = 0;
for (int j = mask; j; j >>= 1) {
s += j & 1;
}
for (int i = 0; i < n; i++) {
// If the subject i is not assigned and the student s has a preference for subject i
if (preference[s][i] && !(mask & (1 << i))) {
dp[mask] += dp[mask | (1 << i)];
}
}
}
return dp[0];
}
int main() {
int n;
printf("How many students are there?\n");
scanf("%d", &n);
int preference[n][n];
printf("\nEnter the preferences of each of %d students for %d subjects\n", n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &preference[i][j]);
}
}
printf("\nTotal number of assignments that can be prepared are \n");
printf("%lld\n", assign(n, preference));
return 0;
}