-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3405
More file actions
40 lines (31 loc) · 869 Bytes
/
3405
File metadata and controls
40 lines (31 loc) · 869 Bytes
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
int mod=1e9+7;
class Solution {
public:
long long findPow(long long a,long long b){
if (b==0) return 1;
long long half=findPow(a,b/2);
long long res=(half*half)%mod;
if (b%2==1) {
res=(res*a)%mod;
}
return res;
}
int countGoodArrays(int n, int m, int k) {
vector<long long>fact(n+1);
vector<long long>invFact(n+1,1);
fact[0]=1;
fact[1]=1;
for (int i=2;i<=n;i++){
fact[i]=(i*fact[i-1])%mod;
}
for (int i=0;i<=n;i++){
invFact[i]=findPow(fact[i],mod-2)%mod;
}
long long res1=(fact[n-1]%mod*invFact[k]%mod*invFact[n-k-1]%mod)%mod;
long long res2=(res1*m)%mod;
long long finalResult=(res2*findPow(m-1,n-k-1)%mod)%mod;
return finalResult;
}
};
TC:O(N+LOG(N-K-1))
SC:O(N)