A: Basic Arithmetic and Output
Given integers a, b, c, d, compute (a + b) * (c - d) and output the result followed by the string "Takahashi".
int a = input(), b = input(), c = input(), d = input();
cout << (a + b) * (c - d) << endl;
cout << "Takahashi" << endl;
Time complexity: O(1)
B: Finding Corners in a Grid
Given a grid of characters, locate the top-left and bottom-right corners of a rectangular region where '#' marks the boundaries.
char grid[MAX][MAX];
int top_x = -1, top_y = -1, bot_x = -1, bot_y = -1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (grid[i][j] == '#') {
if (grid[i-1][j] != '#' && grid[i][j-1] != '#' && grid[i-1][j-1] != '#') {
top_x = i; top_y = j;
}
if (grid[i+1][j] != '#' && grid[i][j+1] != '#' && grid[i+1][j+1] != '#') {
bot_x = i; bot_y = j;
}
}
}
}
cout << top_x << " " << top_y << endl << bot_x << " " << bot_y << endl;
Time complexity: O(n²)
C: Subset Generation via Bitmasking
Given a number n, generate all x such that x OR n equals n (i.e., all subset of the set bits in n).
long long n = input();
vector<long long=""> subsets;
for (long long i = n; i; i = (i - 1) & n) {
subsets.push_back(i);
}
subsets.push_back(0);
reverse(subsets.begin(), subsets.end());
for (auto x : subsets) cout << x << endl;
</long>
Time complexity: O(2^(popcount(n)))
D: Connected Components in a Hexagonal Grid
Given a grid where '#' represents nodes, count the number of connected components considering hexagonal adjacency (six directions).
int grid[5005][5005], component_count = 0;
int dx[] = {-1, -1, 0, 0, 1, 1};
int dy[] = {-1, 0, -1, 1, 0, 1};
void dfs(int x, int y) {
if (grid[x][y] != 1) return;
grid[x][y] = 2;
for (int k = 0; k < 6; k++) {
dfs(x + dx[k], y + dy[k]);
}
}
int main() {
// Read input and mark positions
for (int i = 0; i < n; i++) {
int x = input() + 1010, y = input() + 1010;
grid[x][y] = 1;
}
for (int i = 1; i < 2050; i++) {
for (int j = 1; j < 2050; j++) {
if (grid[i][j] == 1) {
component_count++;
dfs(i, j);
}
}
}
cout << component_count << endl;
}
Time complexity: O(n²)
E: Binary Search for Unique Row and Column
Given a matrix with exactly one row and one column that do not sum to 1, locate these indices using binary search with range queries.
int n = input();
int low_r = 1, high_r = n, mid_r;
while (low_r < high_r) {
mid_r = (low_r + high_r) / 2;
cout << "? " << low_r << " " << mid_r << " " << 1 << " " << n << endl;
fflush(stdout);
int response = input();
if (response != (mid_r - low_r + 1)) {
high_r = mid_r;
} else {
low_r = mid_r + 1;
}
}
int row_ans = low_r;
int low_c = 1, high_c = n, mid_c;
while (low_c < high_c) {
mid_c = (low_c + high_c) / 2;
cout << "? " << 1 << " " << n << " " << low_c << " " << mid_c << endl;
fflush(stdout);
int response = input();
if (response != (mid_c - low_c + 1)) {
high_c = mid_c;
} else {
low_c = mid_c + 1;
}
}
int col_ans = low_c;
cout << "! " << row_ans << " " << col_ans << endl;
fflush(stdout);
Query complexity: O(log n)
F: Summation Over a Grid with Alternating Pattern
Compute the sum of grid values in a rectangle where each element (i-1)*m + j is included only if (i+j-1) is odd.
const int MOD = 998244353;
long long n, m, q;
long long compute_range(long long x, long long y) {
if (x <= 0 || y <= 0) return 0;
long long full_rows = x / 2;
long long base = ((full_rows * m) % MOD + (full_rows * (full_rows + 1)) % MOD) % MOD;
long long step = (2 * m * full_rows) % MOD;
long long sum_full = (base + (base + step * (full_rows - 1)) % MOD * full_rows % MOD * inv(2, MOD) % MOD;
if (x % 2 == 1) {
long long last_row = (x / 2) * (x / 2 - 1) % MOD;
last_row = (last_row + (x / 2) * ((y - 1) * m + 1) % MOD) % MOD;
sum_full = (sum_full + last_row) % MOD;
}
return sum_full;
}
int main() {
cin >> n >> m >> q;
while (q--) {
long long a, b, c, d;
cin >> a >> b >> c >> d;
long long result = (compute_range(b, d) - compute_range(a-1, d) - compute_range(b, c-1) + compute_range(a-1, c-1)) % MOD;
cout << (result + MOD) % MOD << endl;
}
}
Time complexity: O(q)
G: Dynamic Programming with Limited Value Range
For each k from 0 to m, choose between A_i and B_i for each element to achieve sum k with minimal selections from B, given that sum(A) + sum(B) = m.
Let diff_i = B_i - A_i and base = sum(A). Use dynamic programming where state f[i][x] represents the minimal changes to achieve sum x after i elements.
Key observations: The total absolute differences sum to at most m, and the number of distinct differences is O(√m).
// Conceptual code outline - full implementation depends on input constraints
vector<int> dp(m+1, INF);
dp[base] = 0;
for (int i = 0; i < n; i++) {
vector<int> next_dp = dp;
for (int j = 0; j <= m; j++) {
if (dp[j] != INF && j + diff[i] >= 0 && j + diff[i] <= m) {
next_dp[j + diff[i]] = min(next_dp[j + diff[i]], dp[j] + 1);
}
}
dp = move(next_dp);
}
for (int k = 0; k <= m; k++) {
cout << (dp[k] == INF ? -1 : dp[k]) << endl;
}
</int></int>
Time complexity: O(n * m)