Skip to content

Commit 06b31ae

Browse files
authored
Create day2sol2
1 parent b91b85c commit 06b31ae

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

  • Problems/Mathematics/Day-02/sol/Sujal_Kshatri
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// submission llink - https://codeforces.com/contest/1475/submission/355598230
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
static const long long MOD = 1000000007;
7+
8+
/*
9+
Approach:
10+
1. To maximize the total number of followers, we must select the k bloggers
11+
with the largest follower counts.
12+
2. Sort the array in descending order.
13+
3. Let `threshold` be the k-th largest value (a[k-1] after sorting).
14+
4. All values greater than `threshold` must be selected.
15+
5. Among the bloggers with follower count equal to `threshold`:
16+
- `need` = how many of them appear in the top k positions
17+
- `total` = how many times `threshold` appears in the entire array
18+
6. The number of ways to choose these bloggers is:
19+
C(total, need)
20+
7. Use factorials and modular inverses to compute combinations efficiently
21+
modulo 1e9+7.
22+
*/
23+
24+
long long modpow(long long a, long long b){
25+
long long res = 1;
26+
while(b){
27+
if(b & 1) res = res * a % MOD;
28+
a = a * a % MOD;
29+
b >>= 1;
30+
}
31+
return res;
32+
}
33+
34+
int main(){
35+
36+
const int MAXN = 1000;
37+
vector<long long> fact(MAXN + 1), invfact(MAXN + 1);
38+
39+
fact[0] = 1;
40+
for(int i = 1; i <= MAXN; i++)
41+
fact[i] = fact[i - 1] * i % MOD;
42+
43+
invfact[MAXN] = modpow(fact[MAXN], MOD - 2);
44+
for(int i = MAXN; i > 0; i--)
45+
invfact[i - 1] = invfact[i] * i % MOD;
46+
47+
auto comb = [&](int n, int r){
48+
if(r < 0 || r > n) return 0LL;
49+
return fact[n] * invfact[r] % MOD * invfact[n - r] % MOD;
50+
};
51+
52+
int t;
53+
cin >> t;
54+
while(t--){
55+
int n, k;
56+
cin >> n >> k;
57+
58+
vector<int> a(n);
59+
for(int i = 0; i < n; i++) cin >> a[i];
60+
61+
sort(a.begin(), a.end(), greater<int>());
62+
63+
int threshold = a[k - 1];
64+
int need = 0, total = 0;
65+
66+
for(int i = 0; i < k; i++)
67+
if(a[i] == threshold) need++;
68+
69+
for(int i = 0; i < n; i++)
70+
if(a[i] == threshold) total++;
71+
72+
cout << comb(total, need) << "\n";
73+
}
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)