-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathComputeNcrModP.java
More file actions
179 lines (119 loc) · 3.05 KB
/
ComputeNcrModP.java
File metadata and controls
179 lines (119 loc) · 3.05 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Compute nCr % m
Problem Description
Given three integers A, B and C, where A represents n, B represents r and C represents m, find and return the value of nCr % m where nCr % m = (n!/((n-r)!*r!))% m.
x! means factorial of x i.e. x! = 1 * 2 * 3... * x.
Problem Constraints
1 <= A * B <= 106
1 <= B <= A
1 <= C <= 106
Input Format
The first argument given is integer A ( = n).
The second argument given is integer B ( = r).
The third argument given is integer C ( = m).
Output Format
Return the value of nCr % m.
Example Input
Input 1:
A = 5
B = 2
C = 13
Input 2:
A = 6
B = 2
C = 13
Example Output
Output 1:
10
Output 2:
2
Example Explanation
Explanation 1:
The value of 5C2 % 11 is 10.
Explanation 2:
The value of 6C2 % 13 is 2.
*/
/*
Solution Approach
If we calculate nCr by calculating factorial of each number and then doing n! / (r! * (n-r)!) % m. This will not work as the factorial can be very large and will cause overflow.
As we know nCr = n-1Cr-1 + n-1Cr.
So we will use Dynamic Programming approach and calculate the value of nCr.
*/
// A modular inverse based solution to
// compute nCr %
import java.io.*;
class GFG {
public static long fact[],inverseFact[],sum[];
final static long c=(long)(1E9+7);
public static long fastExponentiation(long a,long b)
{
long res=1;
while(b>0)
{
while(b%2==0)
{
a=(a*a)%c;
b=b>>1;
}
b--;
res=(res*a)%c;
}
return res;
}
public static long power(long x,long y)
{
if (y == 0)
return 1;
long p = power(x, y/2) % c;
p = (p * p) % c;
return (y%2 == 0)? p : (x * p) % c;
}
public static void initialise()
{
fact=new long[200001];
inverseFact=new long[200001];
storeFactorials(200000);
storeReverseFactorials(200000);
}
public static void storeFactorials(int n)
{
n++;
fact[0]=1;
for(int i=1;i<n;++i)
fact[i]=(fact[i-1]*i)%c;
}
public static void storeReverseFactorials(int n)
{
n++;
inverseFact[0]=inverseFact[1]=1;
for(int i=2;i<n;++i)
inverseFact[i]=power(fact[i],c-2);
}
public static long combo(int n,int k)
{
if(n<k||n<0||k<0)
return 0;
else
return ((fact[n]*((inverseFact[k]*inverseFact[n-k])%c))%c);
}
public static void main (String[] args) {
initialise();
System.out.println(combo(200000, 100000));
}
}
public class Solution {
public int solve(int A, int B, int C) {
int dp[][] = new int[A+1][B+1];
for (int i = 0; i <= A; i++)
{
for (int j = 0; j <= Integer.min(i, B); j++)
{
if (j == 0 || j == i)
dp[i][j] = 1%C;
else
dp[i][j] = (dp[i-1][j-1] + dp[i-1][j])%C;
}
}
return dp[A][B];
}
}