A – Two Rectangles [Max Area Logic]
Given the dimensions of two rectangles:
- Rectangle 1: (A \times B)
- Rectangle 2: (C \times D)
Determine and output the larger area. Tie-breaking is irrelevant.
i64 a, b, c, d;
std::cin >> a >> b >> c >> d;
i64 r1 = a * b, r2 = c * d;
i64 best = (r1 >= r2) ? r1 : r2;
std::cout << best << "\n";
B – Increment / Decrement [Maximum Value During Operation]
An integer (x) starts at 0. You are given a string (S) of length (N) consisting of characters I and D. For each character:
Iincreases (x) by 1Ddecreases (x) by 1
Report the maximum value (x) ever attains (including the initial state).
int cur = 0, peak = 0;
int N;
std::string ops;
std::cin >> N >> ops;
for (char c : ops) {
if (c == 'I') cur++;
else cur--;
if (cur > peak) peak = cur;
}
std::cout << peak << "\n";
C – Factorial Factors [Counting Divisors Modulo 1e9+7]
For a given integer (N) ((1 \le N \le 1000)), find the number of positive divisors of (N!) modulo (10^9+7).
Approach: Express (N!) via prime factorization: (N! = \prod p_i^{e_i}). The divisor count is (d(N!) = \prod (e_i + 1)).
We can accumulate the exponents for each prime up to (N) by iterating from (2) to (N) and decomposing each integer.
#include <iostream>
#include <vector>
const int MAXN = 1005;
const int MOD = 1000000007;
std::vector<int> primes;
std::vector<int> minPrime(MAXN, 0);
void sieve(int limit) {
for (int i = 2; i <= limit; ++i) {
if (minPrime[i] == 0) {
minPrime[i] = i;
primes.push_back(i);
}
for (int p : primes) {
if (p > minPrime[i] || i * p > limit) break;
minPrime[i * p] = p;
}
}
}
int main() {
int N;
std::cin >> N;
if (N < 2) {
std::cout << 1 << "\n";
return 0;
}
sieve(N);
std::vector<int> expCount(N + 1, 0);
for (int val = 2; val <= N; ++val) {
int tmp = val;
for (int p : primes) {
if (p * p > tmp) break;
while (tmp % p == 0) {
expCount[p]++;
tmp /= p;
}
}
if (tmp > 1) expCount[tmp]++;
}
long long ans = 1;
for (int p : primes) {
if (expCount[p] > 0)
ans = (ans * (expCount[p] + 1)) % MOD;
}
std::cout << ans << "\n";
}
D – Walk and Teleport [Minimum Fatigue DP]
There are (N) towns arranged west to east at coordinates (X_1 < X_2 < \dots < X_N). You begin at town 1 and must visit all town in order.
Two movement options exist:
- Walk from the previous town: fatigue increases by (A \times \text{distance}).
- Teleport from anywhere: fatigue increases by (B) (distance independent).
Find the minimal total fatigue.
Dynamic Programming: Let (\text{dpWalk}) and (\text{dpTele}) represent the minimal fatigue to reach the current town via walking or teleporting respectively. Initial values at town 1 are zero.
Transition:
nextWalk = min(dpWalk, dpTele) + A * (X_i - X_{i-1})
nextTele = min(dpWalk, dpTele) + B
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int N;
long long A, B;
std::cin >> N >> A >> B;
std::vector<long long> pos(N + 1);
for (int i = 1; i <= N; ++i) std::cin >> pos[i];
long long dpWalk = 0, dpTele = 0;
for (int i = 2; i <= N; ++i) {
long long base = std::min(dpWalk, dpTele);
long long nextWalk = base + A * (pos[i] - pos[i - 1]);
long long nextTele = base + B;
dpWalk = nextWalk;
dpTele = nextTele;
}
std::cout << std::min(dpWalk, dpTele) << "\n";
}