Skip to content

Commit 8fb76cd

Browse files
committed
Fix: Solved Mathematics Day-02 q002 (Issue #185)
1 parent 6d8d4e3 commit 8fb76cd

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Problem:
3+
Pick k bloggers from n to maximize total followers, and count how many ways we can do this.
4+
The answer should be modulo 10^9 + 7.
5+
6+
Approach:
7+
To maximize the sum, we must pick bloggers with the highest follower counts.
8+
I sort the array in descending order. The "threshold" value will be the followers of the k-th blogger.
9+
I then count how many bloggers in the entire list have this threshold value and how many
10+
we actually need to pick from them to complete our k-blogger selection.
11+
The result is simply (total_with_threshold) Choose (needed_from_them).
12+
I used Pascal's triangle to precompute combinations since n is small (up to 1000).
13+
14+
TimeComplexity: O(n^2) precompute + O(n log n) per test case
15+
Spacecomplexity: O(nCr table size) approx O(n^2)
16+
17+
Submission Link: https://codeforces.com/contest/1475/submission/355409229
18+
*/
19+
20+
#include <iostream>
21+
#include <vector>
22+
#include <algorithm>
23+
24+
using namespace std;
25+
26+
long long nCr[1005][1005];
27+
const int MOD = 1e9 + 7;
28+
29+
void pre() {
30+
for (int i = 0; i <= 1000; i++) {
31+
nCr[i][0] = 1;
32+
for (int j = 1; j <= i; j++) {
33+
nCr[i][j] = (nCr[i - 1][j - 1] + nCr[i - 1][j]) % MOD;
34+
}
35+
}
36+
}
37+
38+
void solve() {
39+
int n, k;
40+
cin >> n >> k;
41+
42+
vector<int> a(n);
43+
for (int i = 0; i < n; i++) cin >> a[i];
44+
45+
sort(a.begin(), a.end(), greater<int>());
46+
47+
int target = a[k - 1];
48+
int cnt = 0;
49+
for (int x : a) {
50+
if (x == target) cnt++;
51+
}
52+
53+
int take = 0;
54+
for (int i = 0; i < k; i++) {
55+
if (a[i] == target) take++;
56+
}
57+
58+
cout << nCr[cnt][take] << endl;
59+
}
60+
61+
int main() {
62+
ios::sync_with_stdio(0);
63+
cin.tie(0);
64+
65+
pre();
66+
67+
int t;
68+
cin >> t;
69+
while (t--) {
70+
solve();
71+
}
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)