Counting Valid 2x2 Submatrices and Optimizing Zero-Prefix Products

To solve this problem, we scan every possible 2x2 submatrix in a n × m grid and check whether the four characters within it collectively contain at least one 'y', one 'o', and one 'u'. The order does not matter—only the presence of all three required characters.

The algorithm iterates over all valid top-left corners of 2x2 blocks (from row 1 to n-1 and column 1 to m-1), extracts the four characters, and uses string search to verify inclusion. If all three characters are found, the counter is incremented.

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<string> grid(n);
    for (auto &row : grid) cin >> row;

    int count = 0;
    for (int i = 1; i < n; ++i) {
        for (int j = 1; j < m; ++j) {
            string quad = string(1, grid[i][j]) +
                         grid[i][j - 1] +
                         grid[i - 1][j] +
                         grid[i - 1][j - 1];
            if (quad.find('y') != string::npos &&
                quad.find('o') != string::npos &&
                quad.find('u') != string::npos) {
                ++count;
            }
        }
    }
    cout << count << '\n';
    return 0;
}

Minimum Cost to Alternate 0s and 1s in a Binary String

We are given a binary string and asked to transform it into an alternating sequence (e.g., "0101..." or "1010...") with minimal cost. The cost of flipping the character at position i (1-indexed) is i.

We use dynamic programming where dp[i][0] represents the minimum cost to make the prefix ending at position i end with '0', and similarly dp[i][1] for ending with '1'. The recurrence is:

  • If current char is '0', then to end with '1', we must flip it: cost += i
  • If current char is '1', then to end with '0', we must flip it: cost += i

Transition: dp[i][0] = dp[i-1][1] + cost_to_make_0_at_i

#include <bits/stdc++.h>
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;
    s = " " + s; // 1-indexed alignment
    int len = s.size();
    vector<vector<i64>> dp(len, vector<i64>(2, 0));

    for (int i = 1; i < len; ++i) {
        int bit = s[i] - '0';
        dp[i][0] = dp[i - 1][1] + (bit ? i : 0);      // make current 0
        dp[i][1] = dp[i - 1][0] + (bit ? 0 : i);      // make current 1
    }

    cout << min(dp[len - 1][0], dp[len - 1][1]) << '\n';
    return 0;
}

Range of Operations to Reach a Target Sum

Given integers a, b, l, and r, determine the minimum and maximum number of operations needed to transform a into b, where each operation adds a value in the range [l, r].

The minimal number of steps is achieved by always adding the maximum value r: ceil((b - a) / r). The maximum number of steps is achieved by always adding the minimum value l: floor((b - a) / l). If the minimal required steps exceed the maximum possible steps, the transformation is impossible.

Edge case: if b < a, no solution exists. The problem assumes b >= a.

#include <bits/stdc++.h>
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) {
        int a, b, l, r;
        cin >> a >> b >> l >> r;

        if (b < a) {
            cout << -1 << '\n';
            continue;
        }

        int min_ops = (b - a + r - 1) / r;  // ceil((b-a)/r)
        int max_ops = (b - a) / l;          // floor((b-a)/l)

        if (min_ops > max_ops) {
            cout << -1 << '\n';
        } else {
            cout << min_ops << ' ' << max_ops << '\n';
        }
    }
    return 0;
}

Counting Pairs with Product Ending in at Least x Zeros

A product ends in x zeros if and only if it contains at least x factors of 10, i.e., at least x pairs of 2 and 5.

For each number, we precompute how many times it can be divided by 2 and by 5. Then, for each number, we look back at all previously seen numbers and check whether their combined 2s and 5s meet or exceed x.

To optimize, we use a 2D frequancy array freq[2s][5s] to store how many numbers have exactly 2s factors of 2 and 5s factors of 5. Since 1e9 < 2^30 and 5^13 < 1e9, bounds of 32×32 are sufficient.

#include <bits/stdc++.h>
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, x;
    cin >> n >> x;
    i64 result = 0;
    int freq[32][32] = {0};

    for (int i = 0; i < n; ++i) {
        int num;
        cin >> num;

        int count2 = 0, count5 = 0;
        while (num % 2 == 0) {
            ++count2;
            num /= 2;
        }
        while (num % 5 == 0) {
            ++count5;
            num /= 5;
        }

        // Check all previously stored pairs
        for (int p2 = 0; p2 < 32; ++p2) {
            for (int p5 = 0; p5 < 32; ++p5) {
                if (count2 + p2 >= x && count5 + p5 >= x) {
                    result += freq[p2][p5];
                }
            }
        }

        freq[count2][count5]++;
    }

    cout << result << '\n';
    return 0;
}

Tags: dynamic-programming number-theory binary-string factorization grid-submatrix

Posted on Wed, 24 Jun 2026 17:58:58 +0000 by webdes03