Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Flexible String Revisit

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

constexpr int P = 998244353;
using Mint = MInt<P>;

int solve(string &a, string &b) {
    int n = a.length();

    vector<Mint> dp(n + 1), A(n + 1), B(n + 1);
    // dp[i] is the expected moves when there are i mismatches b/w the strings.

    // Every dp[i] would be a linear function of the form Ax + B.
    // Let's maintain 2 arrays A and B to store this linear function.
    A[0] = 0, B[0] = 0; // 0*x + 0
    A[1] = 1, B[1] = 0; // 1*x + 0
    for (int i = 1; i < n; i++) {
        // dp[i] = 1 + (1 - p)*dp[i - 1] + p*dp[i + 1]
        // p*dp[i + 1] = dp[i] - (1 - p)*dp[i - 1] - 1;
        // p is the probability of picking up matched entry.
        //
        // p = (n - i)/n
        // p will never be zero, so we can divide freely.

        Mint p = (Mint)(n - i) / n;
        Mint denom_inv = 1 / p;
        A[i + 1] = denom_inv * (A[i] - (1 - p) * A[i - 1]);
        B[i + 1] = denom_inv * (B[i] - (1 - p) * B[i - 1] - 1);
    }

    // Now, dp[n] would be populated as a linear function.
    // However, there's a different way to populate dp[n].
    // dp[n] = 1 + dp[n - 1]
    // Hence, both these value should be equal.
    //
    // Ex + F = 1 + Px + Q
    // x = (1 + Q - F)/(E - P)
    Mint x = (1 + B[n - 1] - B[n]) / (A[n] - A[n - 1]);

    for (int i = 0; i <= n; i++) {
        dp[i] = A[i] * x + B[i];
    }

    int mismatch_count = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            mismatch_count++;
        }
    }

    return dp[mismatch_count].val();
}

int main() {
    int t;
    cin >> t;

    for (int zz = 0; zz < t; zz++) {
        int n;
        cin >> n;
        string a, b;
        cin >> a >> b;
        cout << solve(a, b) << "\n";
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
template <class T> constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

constexpr i64 mul(i64 a, i64 b, i64 p) {
    i64 res = a * b - i64(1.L * a * b / p) * p;
    res %= p;
    if (res < 0) {
        res += p;
    }
    return res;
}
template <i64 P> struct MLong {
    i64 x;
    constexpr MLong() : x{} {}
    constexpr MLong(i64 x) : x{norm(x % getMod())} {}

    static i64 Mod;
    constexpr static i64 getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(i64 Mod_) { Mod = Mod_; }
    constexpr i64 norm(i64 x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr i64 val() const { return x; }
    explicit constexpr operator i64() const { return x; }
    constexpr MLong operator-() const {
        MLong res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MLong inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MLong &operator*=(MLong rhs) & {
        x = mul(x, rhs.x, getMod());
        return *this;
    }
    constexpr MLong &operator+=(MLong rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MLong &operator-=(MLong rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MLong &operator/=(MLong rhs) & { return *this *= rhs.inv(); }
    friend constexpr MLong operator*(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MLong operator+(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MLong operator-(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MLong operator/(MLong lhs, MLong rhs) {
        MLong res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {
        i64 v;
        is >> v;
        a = MLong(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os,
                                              const MLong &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MLong lhs, MLong rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MLong lhs, MLong rhs) {
        return lhs.val() != rhs.val();
    }
};

template <> i64 MLong<0LL>::Mod = i64(1E18) + 9;

template <int P> struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % getMod())} {}

    static int Mod;
    constexpr static int getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_) { Mod = Mod_; }
    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const { return x; }
    explicit constexpr operator int() const { return x; }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

template <> int MInt<0>::Mod = 998244353;

template <int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;
using Mint = MInt<P>;

int solve(string &a, string &b) {
    int n = a.length();

    vector<Mint> dp(n + 1), A(n + 1), B(n + 1);
    // dp[i] is the expected moves when there are i mismatches b/w the strings.

    // Every dp[i] would be a linear function of the form Ax + B.
    // Let's maintain 2 arrays A and B to store this linear function.
    A[0] = 0, B[0] = 0; // 0*x + 0
    A[1] = 1, B[1] = 0; // 1*x + 0
    for (int i = 1; i < n; i++) {
        // dp[i] = 1 + (1 - p)*dp[i - 1] + p*dp[i + 1]
        // p*dp[i + 1] = dp[i] - (1 - p)*dp[i - 1] - 1;
        // p is the probability of picking up matched entry.
        //
        // p = (n - i)/n
        // p will never be zero, so we can divide freely.

        Mint p = (Mint)(n - i) / n;
        Mint denom_inv = 1 / p;
        A[i + 1] = denom_inv * (A[i] - (1 - p) * A[i - 1]);
        B[i + 1] = denom_inv * (B[i] - (1 - p) * B[i - 1] - 1);
    }

    // Now, dp[n] would be populated as a linear function.
    // However, there's a different way to populate dp[n].
    // dp[n] = 1 + dp[n - 1]
    // Hence, both these value should be equal.
    //
    // Ex + F = 1 + Px + Q
    // x = (1 + Q - F)/(E - P)
    Mint x = (1 + B[n - 1] - B[n]) / (A[n] - A[n - 1]);

    for (int i = 0; i <= n; i++) {
        dp[i] = A[i] * x + B[i];
    }

    int mismatch_count = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            mismatch_count++;
        }
    }

    return dp[mismatch_count].val();
}

int main() {
    int t;
    cin >> t;

    for (int zz = 0; zz < t; zz++) {
        int n;
        cin >> n;
        string a, b;
        cin >> a >> b;
        cout << solve(a, b) << "\n";
    }
    return 0;
}