A – Distinct Count
Input three integers. Output how many different values appear.
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> bag;
for (int i = 0; i < 3; ++i) {
int x; cin >> x;
bag.insert(x);
}
cout << bag.size() << '\n';
}
B – Coloring a Line of Balls
Given N balls in a row and K colors, count colorings such that no two adjacent balls share the same color.
Let f(n) be the answer for n balls.
- First ball:
Kchoices. - Each subsequent ball:
K-1choices.
Hence
f(n) = K * (K-1)^(n-1)
Circular variant
When the balls form a ring, use inclusion–exclusion.
Let g(n) be the number of colorings of n balls in a row with adjacent colors different and the first and last also different. Then
g(2) = K*(K-1)
g(n) = (K-1)^n + (-1)^n * (K-1) (for n ≥ 2)
A linear-time recurrence is
g[n] = (K-1) * (g[n-1] + g[n-2])
with base cases g[1] = 0, g[2] = K*(K-1).
C – Minimum Voters for Monotonic Ratios
Process N given ratios A_i : B_i in order. After each step the actual vote counts must be non-decreasing and maintain the exact ratio. Find the smallest total voters.
Maintain current counts (x, y) initialized to (0, 0).
For each ratio (a, b) compute the smallest multiplier k such that
k * a ≥ x and k * b ≥ y
Update
k = max((x + a - 1)/a, (y + b - 1)/b);
x = k * a;
y = k * b;
Finally output x + y.
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N; cin >> N;
int64 x = 0, y = 0;
for (int i = 0; i < N; ++i) {
int a, b; cin >> a >> b;
int64 k = max((x + a - 1) / a, (y + b - 1) / b);
x = k * a;
y = k * b;
}
cout << x + y << '\n';
}
D – Modified Rock-Paper-Scissors
n rounds, only rock (g) and paper (p) allowed. After all rounds each player must have played rock atleast as many times as paper. Given B's moves in a string s, maximize A's score (win +1, lose –1, draw 0).
Key observations:
Acan always avoid negative score by copyingB. 2 Letrockbe the count of'g'ins, andpaper = n - rock.
Initially let A copy B. The net score is 0.
Changing a round where B plays 'g' from g to p gains 1 point (paper beats rock) but consumes one extra rock quota. The maximum number of such beneficial switches is
max_gain = (rock - paper) / 2
Hence the optimal score is exactly this value.
#include <bits/stdc++.h>
using namespace std;
int main() {
string s; cin >> s;
int rock = count(s.begin(), s.end(), 'g');
int paper = s.size() - rock;
cout << (rock - paper) / 2 << '\n';
}