QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#280613#7789. Outro: True Love Waitsucup-team133#WA 8ms12988kbC++1748.7kb2023-12-09 17:15:502023-12-09 17:15:50

Judging History

你现在查看的是最新测评结果

  • [2023-12-09 17:15:50]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:12988kb
  • [2023-12-09 17:15:50]
  • 提交

answer

// -fsanitize=undefined,
// #define _GLIBCXX_DEBUG


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <bitset>
#include <chrono>


#include <utility>

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m < 2^31`
    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }
    static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }
    dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


#include <algorithm>
#include <array>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <type_traits>
#include <vector>

namespace atcoder {

namespace internal {

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly(std::vector<mint>& a) {
    static constexpr int g = internal::primitive_root<mint::mod()>;
    int n = int(a.size());
    int h = internal::ceil_pow2(n);

    static bool first = true;
    static mint sum_e[30];  // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i]
    if (first) {
        first = false;
        mint es[30], ies[30];  // es[i]^(2^(2+i)) == 1
        int cnt2 = bsf(mint::mod() - 1);
        mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
        for (int i = cnt2; i >= 2; i--) {
            // e^(2^i) == 1
            es[i - 2] = e;
            ies[i - 2] = ie;
            e *= e;
            ie *= ie;
        }
        mint now = 1;
        for (int i = 0; i <= cnt2 - 2; i++) {
            sum_e[i] = es[i] * now;
            now *= ies[i];
        }
    }
    for (int ph = 1; ph <= h; ph++) {
        int w = 1 << (ph - 1), p = 1 << (h - ph);
        mint now = 1;
        for (int s = 0; s < w; s++) {
            int offset = s << (h - ph + 1);
            for (int i = 0; i < p; i++) {
                auto l = a[i + offset];
                auto r = a[i + offset + p] * now;
                a[i + offset] = l + r;
                a[i + offset + p] = l - r;
            }
            now *= sum_e[bsf(~(unsigned int)(s))];
        }
    }
}

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly_inv(std::vector<mint>& a) {
    static constexpr int g = internal::primitive_root<mint::mod()>;
    int n = int(a.size());
    int h = internal::ceil_pow2(n);

    static bool first = true;
    static mint sum_ie[30];  // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i]
    if (first) {
        first = false;
        mint es[30], ies[30];  // es[i]^(2^(2+i)) == 1
        int cnt2 = bsf(mint::mod() - 1);
        mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
        for (int i = cnt2; i >= 2; i--) {
            // e^(2^i) == 1
            es[i - 2] = e;
            ies[i - 2] = ie;
            e *= e;
            ie *= ie;
        }
        mint now = 1;
        for (int i = 0; i <= cnt2 - 2; i++) {
            sum_ie[i] = ies[i] * now;
            now *= es[i];
        }
    }

    for (int ph = h; ph >= 1; ph--) {
        int w = 1 << (ph - 1), p = 1 << (h - ph);
        mint inow = 1;
        for (int s = 0; s < w; s++) {
            int offset = s << (h - ph + 1);
            for (int i = 0; i < p; i++) {
                auto l = a[i + offset];
                auto r = a[i + offset + p];
                a[i + offset] = l + r;
                a[i + offset + p] =
                    (unsigned long long)(mint::mod() + l.val() - r.val()) *
                    inow.val();
            }
            inow *= sum_ie[bsf(~(unsigned int)(s))];
        }
    }
}

}  // namespace internal

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};
    if (std::min(n, m) <= 60) {
        if (n < m) {
            std::swap(n, m);
            std::swap(a, b);
        }
        std::vector<mint> ans(n + m - 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[i + j] += a[i] * b[j];
            }
        }
        return ans;
    }
    int z = 1 << internal::ceil_pow2(n + m - 1);
    a.resize(z);
    internal::butterfly(a);
    b.resize(z);
    internal::butterfly(b);
    for (int i = 0; i < z; i++) {
        a[i] *= b[i];
    }
    internal::butterfly_inv(a);
    a.resize(n + m - 1);
    mint iz = mint(z).inv();
    for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
    return a;
}

template <unsigned int mod = 998244353,
          class T,
          std::enable_if_t<internal::is_integral<T>::value>* = nullptr>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    using mint = static_modint<mod>;
    std::vector<mint> a2(n), b2(m);
    for (int i = 0; i < n; i++) {
        a2[i] = mint(a[i]);
    }
    for (int i = 0; i < m; i++) {
        b2[i] = mint(b[i]);
    }
    auto c2 = convolution(move(a2), move(b2));
    std::vector<T> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        c[i] = c2[i].val();
    }
    return c;
}

std::vector<long long> convolution_ll(const std::vector<long long>& a,
                                      const std::vector<long long>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    static constexpr unsigned long long MOD1 = 754974721;  // 2^24
    static constexpr unsigned long long MOD2 = 167772161;  // 2^25
    static constexpr unsigned long long MOD3 = 469762049;  // 2^26
    static constexpr unsigned long long M2M3 = MOD2 * MOD3;
    static constexpr unsigned long long M1M3 = MOD1 * MOD3;
    static constexpr unsigned long long M1M2 = MOD1 * MOD2;
    static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;

    static constexpr unsigned long long i1 =
        internal::inv_gcd(MOD2 * MOD3, MOD1).second;
    static constexpr unsigned long long i2 =
        internal::inv_gcd(MOD1 * MOD3, MOD2).second;
    static constexpr unsigned long long i3 =
        internal::inv_gcd(MOD1 * MOD2, MOD3).second;

    auto c1 = convolution<MOD1>(a, b);
    auto c2 = convolution<MOD2>(a, b);
    auto c3 = convolution<MOD3>(a, b);

    std::vector<long long> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        unsigned long long x = 0;
        x += (c1[i] * i1) % MOD1 * M2M3;
        x += (c2[i] * i2) % MOD2 * M1M3;
        x += (c3[i] * i3) % MOD3 * M1M2;
        // B = 2^63, -B <= x, r(real value) < B
        // (x, x - M, x - 2M, or x - 3M) = r (mod 2B)
        // r = c1[i] (mod MOD1)
        // focus on MOD1
        // r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B)
        // r = x,
        //     x - M' + (0 or 2B),
        //     x - 2M' + (0, 2B or 4B),
        //     x - 3M' + (0, 2B, 4B or 6B) (without mod!)
        // (r - x) = 0, (0)
        //           - M' + (0 or 2B), (1)
        //           -2M' + (0 or 2B or 4B), (2)
        //           -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1)
        // we checked that
        //   ((1) mod MOD1) mod 5 = 2
        //   ((2) mod MOD1) mod 5 = 3
        //   ((3) mod MOD1) mod 5 = 4
        long long diff =
            c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
        if (diff < 0) diff += MOD1;
        static constexpr unsigned long long offset[5] = {
            0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
        x -= offset[diff % 5];
        c[i] = x;
    }

    return c;
}

}  // namespace atcoder



using namespace std;
using namespace atcoder;


#define rep(i,n) for (int i=0;i<n;i+=1)
#define rrep(i,n) for (int i=n-1;i>-1;i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << " )\n";

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;


template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
void printv(vec<T> x){
  for (auto e:x){
    cout<<e<<" ";
  }
  cout<<endl;
}



template<class T,class U>
ostream& operator<<(ostream& os, const pair<T,U>& A){
  os << "(" << A.first <<", " << A.second << ")";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const set<T>& S){
  os << "set{";
  for (auto a:S){
    os << a;
    auto it = S.find(a);
    it++;
    if (it!=S.end()){
      os << ", ";
    }
  }
  os << "}";
  return os;
}

using mint = modint1000000007;

ostream& operator<<(ostream& os, const mint& a){
  os << a.val();
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const vec<T>& A){
  os << "[";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ", ";
    }
  }
  os << "]" ;
  return os;
}

const int M = 100000;

mint g1[M],g2[M],inv[M],finv[M];

void init_mint(){
  g1[0] = 1; g1[1] = 1;
  g2[0] = 1; g2[1] = 1;
  finv[0] = 1; finv[1] = 1;
  inv[1] = 1;
  for (int n=2;n<M;n++){
    g1[n] = g1[n-1] * n;
    inv[n] = (-inv[998244353%n]) * (998244353/n);
    g2[n] = inv[n] * g2[n-1];
    finv[n] = g2[n];
  }
}

mint comb(int n,int r){
  if (r < 0 || n < r) return 0;
  return g1[n] * g2[r] * g2[n-r];
}

vec<int> bigint_quotient(vec<int> A,int b){
  int n = A.size();
  vec<int> Q(n,0);
  int R = 0;
  for (int i=0;i<n;i++){
    R = 10 * R + A[i];
    Q[i] = R/b;
    R = R % b;
  }
  return Q;
}

/**
 *  inverse が普通の逆数。
 *  operator/ とは別であるので注意。
 *  / と % は 最後の要素 つまり 最大次数の係数 が 0 かもしれないので注意
 *  a / b * b + a % b == a (たぶん)
 *
 *  power は2種類
 *  片方は mod c (多項式、遅い)
 *  もう片方は mod x^n
 *
 *  log は a[0] == 1
 *  exp は a[0] == 0
 *  sqrt は a[0] == 1
 *  がそれぞれ必要
 *
 *  sqrt は library checker (https://judge.yosupo.jp/submission/87669) に a[0] != 1 の場合の実装がある(使うか?)
 *
 *  multiply -> 多項式を全て掛ける いわゆる分割統治FFT
 *  evaluate -> a(x) を同時に求める
 *  faulhaber -> f[i] = 1^i + 2^i + ... + up^i
 *  sequence -> (x + 1) * (x + 2) * ... * (x + n)
 *  taylor_shift -> f(x) -> f(x + c)
 *  stirling_number -> OEIS で出てきたら使おうね
 **/

template <typename T> vector<T>& operator+=(vector<T>& a, const vector<T>& b) {
    if (a.size() < b.size()) {
        a.resize(b.size());
    }
    for (int i = 0; i < (int)b.size(); i++) {
        a[i] += b[i];
    }
    return a;
}

template <typename T> vector<T> operator+(const vector<T>& a, const vector<T>& b) {
    vector<T> c = a;
    return c += b;
}

template <typename T> vector<T>& operator-=(vector<T>& a, const vector<T>& b) {
    if (a.size() < b.size()) {
        a.resize(b.size());
    }
    for (int i = 0; i < (int)b.size(); i++) {
        a[i] -= b[i];
    }
    return a;
}

template <typename T> vector<T> operator-(const vector<T>& a, const vector<T>& b) {
    vector<T> c = a;
    return c -= b;
}

template <typename T> vector<T>& operator*=(vector<T>& a, const vector<T>& b) {
    if (a.empty() || b.empty()) {
        a.clear();
    } else {
        vector<T> c = a;
        a = convolution(b,c);
    }
    return a;
}

template <typename T> vector<T> operator*(const vector<T>& a, const vector<T>& b) {
    vector<T> c = a;
    return c *= b;
}

template <typename T> vector<T> inverse(const vector<T>& a) {
    assert(!a.empty() && a[0] != T(0));
    vector<T> h(a);
    int n = (int)h.size();
    T invh0 = T(1) / h[0];
    vector<T> u(1, invh0);
    while ((int)u.size() < n) {
        int t = (int)u.size();
        vector<T> h0;
        for (int i = 0; i < t; i++) {
            h0.emplace_back(i < (int)h.size() ? h[i] : 0);
        }
        vector<T> c = h0 * u;
        vector<T> h1;
        for (int i = t; i < 2 * t; i++) {
            h1.emplace_back(i < (int)h.size() ? h[i] : 0);
        }
        vector<T> aux = u * h1;
        aux.resize(t);
        for (int i = 0; i < t; i++) {
            aux[i] += (i + t < (int)c.size() ? c[i + t] : 0);
        }
        vector<T> v = aux * u;
        v.resize(t);
        for (int i = 0; i < t; i++) {
            u.emplace_back(-v[i]);
        }
    }
    u.resize(n);
    return u;
}

template <typename T> vector<T>& operator/=(vector<T>& a, const vector<T>& b) {
    int n = (int)a.size();
    int m = (int)b.size();
    if (n < m) {
        a.clear();
    } else {
        vector<T> d = b;
        reverse(a.begin(), a.end());
        reverse(d.begin(), d.end());
        d.resize(n - m + 1);
        a *= inverse(d);
        a.erase(a.begin() + n - m + 1, a.end());
        reverse(a.begin(), a.end());
    }
    return a;
}

template <typename T> vector<T> operator/(const vector<T>& a, const vector<T>& b) {
    vector<T> c = a;
    return c /= b;
}

template <typename T> vector<T>& operator%=(vector<T>& a, const vector<T>& b) {
    int n = (int)a.size();
    int m = (int)b.size();
    if (n >= m) {
        vector<T> c = (a / b) * b;
        a.resize(m - 1);
        for (int i = 0; i < m - 1; i++) {
            a[i] -= c[i];
        }
    }
    return a;
}

template <typename T> vector<T> operator%(const vector<T>& a, const vector<T>& b) {
    vector<T> c = a;
    return c %= b;
}

template <typename T, typename U> vector<T> power(const vector<T>& a, const U& b, const vector<T>& c) {
    assert(b >= 0);
    vector<U> binary;
    U bb = b;
    while (bb > 0) {
        binary.emplace_back(bb & 1);
        bb >>= 1;
    }
    vector<T> res = vector<T>{1} % c;
    for (int j = (int)binary.size() - 1; j >= 0; j--) {
        res = res * res % c;
        if (binary[j] == 1) {
            res = res * a % c;
        }
    }
    return res;
}

template <typename T, typename U> vector<T> power(const vector<T>& a, const U& b) {
    assert(b >= 0);
    vector<U> binary;
    U bb = b;
    while (bb > 0) {
        binary.emplace_back(bb & 1);
        bb >>= 1;
    }
    vector<T> res = vector<T>{1};
    for (int j = (int)binary.size() - 1; j >= 0; j--) {
        res = res * res;
        res.resize(a.size());
        if (binary[j] == 1) {
            res = res * a;
            res.resize(a.size());
        }
    }
    return res;
}

template <typename T> vector<T> derivative(const vector<T>& a) {
    vector<T> c = a;
    for (int i = 0; i < (int)c.size(); i++) {
        c[i] *= i;
    }
    if (!c.empty()) {
        c.erase(c.begin());
    }
    return c;
}

template <typename T> vector<T> primitive(const vector<T>& a) {
    vector<T> c = a;
    c.insert(c.begin(), 0);
    for (int i = 1; i < (int)c.size(); i++) {
        c[i] /= i;
    }
    return c;
}

template <typename T> vector<T> logarithm(const vector<T>& a) {
    assert(!a.empty() && a[0] == T(1));
    vector<T> u = primitive(derivative(a) * inverse(a));
    u.resize(a.size());
    return u;
}

template <typename T> vector<T> exponent(const vector<T>& a) {
    assert(!a.empty() && a[0] == T(0));
    int n = (int)a.size();
    vector<T> b = {1};
    while ((int)b.size() < n) {
        vector<T> x(a.begin(), a.begin() + min(a.size(), b.size() << 1));
        x[0] += 1;
        vector<T> old_b = b;
        b.resize(b.size() << 1);
        x -= logarithm(b);
        x *= old_b;
        for (int i = (int)b.size() >> 1; i < (int)min(x.size(), b.size()); i++) {
            b[i] = x[i];
        }
    }
    b.resize(n);
    return b;
}

template <typename T> vector<T> sqrt(const vector<T>& a) {
    assert(!a.empty() && a[0] == T(1));
    int n = (int)a.size();
    vector<T> b = {1};
    while ((int)b.size() < n) {
        vector<T> x(a.begin(), a.begin() + min(a.size(), b.size() << 1));
        b.resize(b.size() << 1);
        x *= inverse(b);
        T inv2 = T(1) / 2;
        for (int i = (int)b.size() >> 1; i < (int)min(x.size(), b.size()); i++) {
            b[i] = x[i] * inv2;
        }
    }
    b.resize(n);
    return b;
}

template <typename T> vector<T> multiply(const vector<vector<T>>& a) {
    if (a.empty()) {
        return {0};
    }
    function<vector<T>(int, int)> mult = [&](int l, int r) {
        if (l == r) {
            return a[l];
        }
        int y = (l + r) >> 1;
        return mult(l, y) * mult(y + 1, r);
    };
    return mult(0, (int)a.size() - 1);
}

template <typename T> T evaluate(const vector<T>& a, const T& x) {
    T res = 0;
    for (int i = (int)a.size() - 1; i >= 0; i--) {
        res = res * x + a[i];
    }
    return res;
}

template <typename T> vector<T> evaluate(const vector<T>& a, const vector<T>& x) {
    if (x.empty()) {
        return {};
    }
    if (a.empty()) {
        return vector<T>(x.size(), 0);
    }
    int n = (int)x.size();
    vector<vector<T>> st((n << 1) - 1);
    function<void(int, int, int)> build = [&](int v, int l, int r) {
        if (l == r) {
            st[v] = vector<T>{-x[l], 1};
        } else {
            int y = (l + r) >> 1;
            int z = v + ((y - l + 1) << 1);
            build(v + 1, l, y);
            build(z, y + 1, r);
            st[v] = st[v + 1] * st[z];
        }
    };
    build(0, 0, n - 1);
    vector<T> res(n);
    function<void(int, int, int, vector<T>)> eval = [&](int v, int l, int r, vector<T> f) {
        f %= st[v];
        if ((int)f.size() < 150) {
            for (int i = l; i <= r; i++) {
                res[i] = evaluate(f, x[i]);
            }
            return;
        }
        if (l == r) {
            res[l] = f[0];
        } else {
            int y = (l + r) >> 1;
            int z = v + ((y - l + 1) << 1);
            eval(v + 1, l, y, f);
            eval(z, y + 1, r, f);
        }
    };
    eval(0, 0, n - 1, a);
    return res;
}

template <typename T> vector<T> interpolate(const vector<T>& x, const vector<T>& y) {
    if (x.empty()) {
        return {};
    }
    assert(x.size() == y.size());
    int n = (int)x.size();
    vector<vector<T>> st((n << 1) - 1);
    function<void(int, int, int)> build = [&](int v, int l, int r) {
        if (l == r) {
            st[v] = vector<T>{-x[l], 1};
        } else {
            int w = (l + r) >> 1;
            int z = v + ((w - l + 1) << 1);
            build(v + 1, l, w);
            build(z, w + 1, r);
            st[v] = st[v + 1] * st[z];
        }
    };
    build(0, 0, n - 1);
    vector<T> m = st[0];
    vector<T> dm = derivative(m);
    vector<T> val(n);
    function<void(int, int, int, vector<T>)> eval = [&](int v, int l, int r, vector<T> f) {
        f %= st[v];
        if ((int)f.size() < 150) {
            for (int i = l; i <= r; i++) {
                val[i] = evaluate(f, x[i]);
            }
            return;
        }
        if (l == r) {
            val[l] = f[0];
        } else {
            int w = (l + r) >> 1;
            int z = v + ((w - l + 1) << 1);
            eval(v + 1, l, w, f);
            eval(z, w + 1, r, f);
        }
    };
    eval(0, 0, n - 1, dm);
    for (int i = 0; i < n; i++) {
        val[i] = y[i] / val[i];
    }
    function<vector<T>(int, int, int)> calc = [&](int v, int l, int r) {
        if (l == r) {
            return vector<T>{val[l]};
        }
        int w = (l + r) >> 1;
        int z = v + ((w - l + 1) << 1);
        return calc(v + 1, l, w) * st[z] + calc(z, w + 1, r) * st[v + 1];
    };
    return calc(0, 0, n - 1);
}

template <typename T> vector<T> faulhaber(const T& up, int n) {
    vector<T> ex(n + 1);
    T e = 1;
    for (int i = 0; i <= n; i++) {
        ex[i] = e;
        e /= i + 1;
    }
    vector<T> den = ex;
    den.erase(den.begin());
    for (auto& d : den) {
        d = -d;
    }
    vector<T> num(n);
    T p = 1;
    for (int i = 0; i < n; i++) {
        p *= up + 1;
        num[i] = ex[i + 1] * (T(1) - p);
    }
    vector<T> res = num * inverse(den);
    res.resize(n);
    T f = 1;
    for (int i = 0; i < n; i++) {
        res[i] *= f;
        f *= i + 1;
    }
    return res;
}

template <typename T> vector<T> sequence(int n) {
    if (n == 0) {
        return {1};
    }
    if (n % 2 == 1) {
        return sequence<T>(n - 1) * vector<T>{n, 1};
    }
    vector<T> c = sequence<T>(n / 2);
    vector<T> a = c;
    reverse(a.begin(), a.end());
    T f = 1;
    for (int i = n / 2 - 1; i >= 0; i--) {
        f *= n / 2 - i;
        a[i] *= f;
    }
    vector<T> b(n / 2 + 1);
    b[0] = 1;
    for (int i = 1; i <= n / 2; i++) {
        b[i] = b[i - 1] * (n / 2) / i;
    }
    vector<T> h = a * b;
    h.resize(n / 2 + 1);
    reverse(h.begin(), h.end());
    f = 1;
    for (int i = 1; i <= n / 2; i++) {
        f /= i;
        h[i] *= f;
    }
    vector<T> res = c * h;
    return res;
}

template <typename T> vector<T> taylor_shift(vector<T> a, T c) {
    int n = (int)a.size();
    vector<T> f(n);
    f[0] = 1;
    for (int i = 1; i < n; i++) {
        f[i] = f[i - 1] * i;
    }
    for (int i = 0; i < n; i++) {
        a[i] *= f[i];
    }
    vector<T> b(n);
    b[0] = 1;
    for (int i = 0; i < n; i++) {
        if (i < n - 1) {
            b[i + 1] = b[i] * c;
        }
        b[i] /= f[i];
    }
    reverse(a.begin(), a.end());
    auto res = a * b;
    res.resize(n);
    reverse(res.begin(), res.end());
    for (int i = 0; i < n; i++) {
        res[i] /= f[i];
    }
    return res;
}

// =====================

vector<mint> stirling_number_1(int n) {
    if (n == 0) {
        return {1};
    }
    if (n == 1) {
        return {0, 1};
    }
    auto f = stirling_number_1(n / 2);
    auto g = taylor_shift(f, -mint(n / 2));
    f = f * g;
    if (n & 1) {
        g = {-(n - 1), 1};
        f = f * g;
    }
    return f;
}

mint bigint_to_mint(vector<int> A){
  mint res = 0;
  for (auto a:A){
    res = res * 10 + mint(a);
  }
  return res;
}

vector<int> bigint_prod(vector<int> A,int b){
  reverse(all(A));
  vector<int> res;
  int Q = 0;
  for (auto a:A){
    Q += a * b;
    res.push_back(Q%10);
    Q /= 10;
  }
  while (Q){
    res.push_back(Q%10);
    Q /= 10;
  }
  reverse(all(res));
  return res;
}

vector<int> bigint_add(vector<int> A,vector<int> B){
  reverse(all(A)); reverse(all(B));
  
  int K = max(int(A.size()),int(B.size()));
  A.resize(K+1);
  B.resize(K+1);
  vector<int> res(K+1);
  int up = 0;
  for (int i=0;i<K+1;i++){
    up += A[i] + B[i];
    res[i] = up % 10;
    up /= 10;
  }
  reverse(all(res));
  return res;
}

template <typename T, size_t N> struct SquareMatrix {
    std::array<std::array<T, N>, N> A;

    SquareMatrix() : A{{}} {}

    size_t size() const { return N; }

    inline const std::array<T, N>& operator[](int k) const { return A[k]; }

    inline std::array<T, N>& operator[](int k) { return A[k]; }

    static SquareMatrix I() {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++) res[i][i] = 1;
        return res;
    }

    SquareMatrix& operator+=(const SquareMatrix& B) {
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                (*this)[i][j] += B[i][j];
            }
        }
        return *this;
    }

    SquareMatrix& operator-=(const SquareMatrix& B) {
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                (*this)[i][j] -= B[i][j];
            }
        }
        return *this;
    }

    SquareMatrix& operator*=(const SquareMatrix& B) {
        std::array<std::array<T, N>, N> C = {};
        for (size_t i = 0; i < N; i++) {
            for (size_t k = 0; k < N; k++) {
                for (size_t j = 0; j < N; j++) {
                    C[i][j] += (*this)[i][k] * B[k][j];
                }
            }
        }
        A.swap(C);
        return *this;
    }

    SquareMatrix& operator*=(const T& v) {
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                (*this)[i][j] *= v;
            }
        }
        return *this;
    }

    SquareMatrix& operator/=(const T& v) {
        T inv = T(1) / v;
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                (*this)[i][j] *= inv;
            }
        }
        return *this;
    }

    SquareMatrix& operator^=(long long k) {
        assert(0 <= k);
        SquareMatrix B = SquareMatrix::I();
        while (k > 0) {
            if (k & 1) B *= *this;
            *this *= *this;
            k >>= 1;
        }
        A.swap(B.A);
        return *this;
    }

    SquareMatrix operator-() const {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                res[i][j] = -(*this)[i][j];
            }
        }
        return res;
    }

    SquareMatrix operator+(const SquareMatrix& B) const { return SquareMatrix(*this) += B; }

    SquareMatrix operator-(const SquareMatrix& B) const { return SquareMatrix(*this) -= B; }

    SquareMatrix operator*(const SquareMatrix& B) const { return SquareMatrix(*this) *= B; }

    SquareMatrix operator*(const T& v) const { return SquareMatrix(*this) *= v; }

    SquareMatrix operator/(const T& v) const { return SquareMatrix(*this) /= v; }

    SquareMatrix operator^(const long long k) const { return SquareMatrix(*this) ^= k; }

    bool operator==(const SquareMatrix& B) const { return A == B.A; }

    bool operator!=(const SquareMatrix& B) const { return A != B.A; }

    SquareMatrix transpose() const {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                res[j][i] = (*this)[i][j];
            }
        }
        return res;
    }

    T determinant() const {
        SquareMatrix B(*this);
        T res = 1;
        for (size_t i = 0; i < N; i++) {
            int pivot = -1;
            for (size_t j = i; j < N; j++) {
                if (B[j][i] != 0) {
                    pivot = j;
                    break;
                }
            }
            if (pivot == -1) return 0;
            if (pivot != (int)i) {
                res *= -1;
                std::swap(B[i], B[pivot]);
            }
            res *= B[i][i];
            T inv = T(1) / B[i][i];
            for (size_t j = 0; j < N; j++) B[i][j] *= inv;
            for (size_t j = i + 1; j < N; j++) {
                T a = B[j][i];
                for (size_t k = 0; k < N; k++) {
                    B[j][k] -= B[i][k] * a;
                }
            }
        }
    }

    SquareMatrix inv() const {
        SquareMatrix B(*this), C = SquareMatrix::I();
        for (size_t i = 0; i < N; i++) {
            int pivot = -1;
            for (size_t j = i; j < N; j++) {
                if (B[j][i] != 0) {
                    pivot = j;
                    break;
                }
            }
            if (pivot == -1) return {};
            if (pivot != (int)i) {
                std::swap(B[i], B[pivot]);
                std::swap(C[i], C[pivot]);
            }
            T inv = T(1) / B[i][i];
            for (size_t j = 0; j < N; j++) {
                B[i][j] *= inv;
                C[i][j] *= inv;
            }
            for (size_t j = 0; j < N; j++) {
                if (j == i) continue;
                T a = B[j][i];
                for (size_t k = 0; k < N; k++) {
                    B[j][k] -= B[i][k] * a;
                    C[j][k] -= C[i][k] * a;
                }
            }
        }
        return C;
    }

    friend std::ostream& operator<<(std::ostream& os, const SquareMatrix& p) {
        os << "[(" << N << " * " << N << " Matrix)";
        os << "\n[columun sums: ";
        for (size_t j = 0; j < N; j++) {
            T sum = 0;
            for (size_t i = 0; i < N; i++) sum += p[i][j];
            ;
            os << sum << (j + 1 < N ? "," : "");
        }
        os << "]";
        for (size_t i = 0; i < N; i++) {
            os << "\n[";
            for (size_t j = 0; j < N; j++) os << p[i][j] << (j + 1 < N ? "," : "");
            os << "]";
        }
        os << "]\n";
        return os;
    }
};

const int MAX_N = 1000100;
mint f[MAX_N];
int g[MAX_N];

mint calc_f_fast(int K){
    if (K<MAX_N){
        return f[K];
    }

    SquareMatrix<mint,2> A;
    A[0] = {4,7}; A[1] = {1,0};
    A = A^(K-2);

    mint res = A[0][0] * 19 + A[0][1] * 3;
    return res;
}


int solve_not_diff(string s,string t,int K){
    if (K == 1){
        return 0;
    }

    return (calc_f_fast(K-1)+1).val();
}

void solve(){
    string s,t;
    int K;
    cin>>s>>t>>K;

    

    int M = max(s.size(),t.size());
    if (M & 1) M += 1;

    string S,T;
    reverse(all(s)); reverse(all(t));

    for (int i=0;i<int(s.size());i++){
        S += s[i];
    }
    for (int i=0;i<M-int(s.size());i++){
        S += '0';
    }

    for (int i=0;i<int(t.size());i++){
        T += t[i];
    }
    for (int i=0;i<M-int(t.size());i++){
        T += '0';
    }

    vector<int> diff(M,0);
    for (int i=0;i<M;i++){
        if (S[i]!=T[i]){
            diff[i] = 1;
        }
    }

    int check_is_diff = accumulate(all(diff),0);

    if (check_is_diff == 0){
        cout << solve_not_diff(s,t,K) << endl;
        return ;
    }

    vector<int> zero_flg(M,1);
    for (int i=0;i+2<M;i+=2){
        zero_flg[i+2] = zero_flg[i] & (diff[i] == 0 && diff[i+1] == 0);
    }

    //debug(diff);

    auto calc = [&](auto self,int top_even_bit,int K)->int {
        assert ((top_even_bit & 1) == 0);
        int a = 2 * diff[top_even_bit+1] + diff[top_even_bit];
        int k = top_even_bit>>1;

        if (top_even_bit == 0){
            if (K!=1) return -1;
            vec<int> ans = {0,1,3,2};
            return ans[a];
        }
        if (a == 0){
            if (zero_flg[top_even_bit]){
                if (g[k] + 1 < K){
                    return -1;
                }
                if (g[k] + 1 == K){
                    return (f[k]+1).val();
                }
                else{
                    return self(self,top_even_bit-2,K);
                }
            }
            else{
                return self(self,top_even_bit-2,K);
            }
        }
        else if (a == 1){
            if (zero_flg[top_even_bit]){
                if (g[k] + 1 < K){
                    return -1;
                }
                if (g[k] + 1 == K){
                    return (f[k]+1+f[k]+2).val();
                }
                else{
                    int val = self(self,top_even_bit-2,K);
                    if (val == -1) return -1;
                    return (mint(val)+f[k]+2).val();
                }
            }
            else{
                int val = self(self,top_even_bit-2,K);
                if (val == -1) return -1;
                return (mint(val)+f[k]+2).val();
            }
        }
        else if (a == 3){
            if (zero_flg[top_even_bit]){
                if (g[k] + 1 < K){
                    return -1;
                }
                if (g[k] + 1 == K){
                    return (f[k]+1+f[k]*2+4).val();
                }
                else{
                    int val = self(self,top_even_bit-2,K);
                    if (val == -1) return -1;
                    return (mint(val)+f[k]*2+4).val();
                }
            }
            else{
                int val = self(self,top_even_bit-2,K);
                if (val == -1) return -1;
                return (mint(val)+f[k]*2+4).val();
            }
        }
        else{
            if (zero_flg[top_even_bit]){
                if (g[k] + 1 < K){
                    return -1;
                }
                if (g[k] + 1 == K){
                    return (f[k-1]+1+f[k]*3+6).val();
                }
                else{
                    int val = self(self,top_even_bit-2,K);
                    if (val == -1) return -1;
                    return (mint(val)+f[k]*3+6).val();
                }
            }
            else{
                int val = self(self,top_even_bit-2,K);
                if (val == -1) return -1;
                return (mint(val)+f[k]*3+6).val();
            }
        }
    };

    cout << calc(calc,M-2,K) << endl;
    return ;


}



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

  f[1] = 3;
  g[1] = 1;
  for (int n=2;n<MAX_N;n++){
    f[n] = f[n-1] * 4 + 7;
    g[n] = g[n-1] + 1;
  }

  int T;
  cin>>T;
  while (T--){
    solve();
  }

  


  
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 12872kb

input:

4
1 10 1
1 10 2
100 0 2
11 11 3

output:

2
-1
9
20

result:

ok 4 number(s): "2 -1 9 20"

Test #2:

score: 0
Accepted
time: 4ms
memory: 12988kb

input:

1
0 0 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 8ms
memory: 12880kb

input:

100
110111 11111 1
10110 101101 1
11010 111111 1
100110 1 1
10010 11010 1
1100 10111 1
100100 111110 1
101110 101100 1
1011 10110 1
110100 1110 1
11010 11000 1
11110 1000 1
111000 11101 1
110 1001 1
101010 11000 1
10 111110 1
110001 101000 1
1010 1000 1
10101 11 1
111011 11010 1
110001 100000 1
1100...

output:

78
59
69
70
15
38
39
3
32
60
3
29
69
12
45
52
37
3
29
64
22
39
54
69
65
27
33
76
34
18
57
13
81
15
23
70
69
36
18
23
29
42
69
54
6
0
63
3
29
15
10
16
80
24
37
59
71
13
23
31
21
34
23
48
21
47
7
44
42
3
37
75
59
29
55
39
29
28
29
70
55
16
54
47
24
18
79
60
8
26
64
58
32
6
8
37
2
68
42
44

result:

ok 100 numbers

Test #4:

score: -100
Wrong Answer
time: 7ms
memory: 12988kb

input:

100
10011111 111 2
1011101100 1000000100 1
100011111 1001001111 1
1001100101 1100100001 1
10101000 10000100 1
1011110101 100011101 1
110100001 111011010 1
1101001100 1111101101 1
1001101 11011010 1
1101110110 1101011000 1
110011001 1100001111 2
1001111001 1011001111 1
1001110 1101110100 2
1110110100...

output:

292
248
788
431
73
930
144
319
283
76
-1
305
-1
-1
86
-1
312
293
1293
433
1179
0
884
963
1215
576
-1
1132
499
811
864
949
1322
406
526
862
-1
447
1203
1238
873
-1
-1
1131
1108
438
134
359
80
740
1057
752
31
950
1093
1261
650
235
996
876
504
925
1344
450
1010
273
-1
1144
1041
717
-1
164
-1
11
798
419...

result:

wrong answer 1st numbers differ - expected: '295', found: '292'