Skip to content

Commit 26d7560

Browse files
authored
Merge pull request #290 from MK-codes365/MK-codes365
Fix: Solve Resistor Time Machine (Issue #286)
2 parents 9f61e91 + ced7953 commit 26d7560

2 files changed

Lines changed: 119 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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
SHORT PROBLEM STATEMENT:
3+
We need to find the minimum number of 1-ohm resistors to get a resistance of a/b.
4+
Resistors can be connected in series or parallel.
5+
6+
APPROACH USING PREFIX SUMS:
7+
Not applicable here as it's a math/GCD based problem.
8+
The logic is basiclly like the Euclidean algorithm. If we have to reach a/b,
9+
we keep taking the floor part (x/y) as series resistors and then
10+
calculate the remainder and swap to simulate the parallel recipricol logic.
11+
12+
TIME & SPACE COMPLEXITY:
13+
Time: O(log(min(a, b)))
14+
Space: O(1)
15+
16+
SUBMISSION LINK: https://codeforces.com/contest/343/submission/355457508
17+
*/
18+
19+
20+
#include <iostream>
21+
#include <algorithm>
22+
23+
using namespace std;
24+
25+
typedef long long ll;
26+
27+
int main() {
28+
ios_base::sync_with_stdio(false);
29+
cin.tie(NULL);
30+
31+
ll x, y;
32+
if (!(cin >> x >> y)) return 0;
33+
34+
ll count = 0;
35+
36+
while (y > 0) {
37+
count += (x / y);
38+
x %= y;
39+
swap(x, y);
40+
}
41+
42+
cout << count << "\n";
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)