QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#279939#7789. Outro: True Love Waitsucup-team1631#WA 1ms3900kbC++239.7kb2023-12-09 12:33:352023-12-09 12:33:35

Judging History

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

  • [2023-12-09 12:33:35]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3900kb
  • [2023-12-09 12:33:35]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

template <int mod>
class Modint {
    using mint = Modint;
    static_assert(mod > 0, "Modulus must be positive");

   public:
    static constexpr int get_mod() noexcept { return mod; }

    constexpr Modint(long long y = 0) noexcept
        : x(y >= 0 ? y % mod : (y % mod + mod) % mod) {}

    constexpr int value() const noexcept { return x; }

    constexpr mint& operator+=(const mint& r) noexcept {
        if ((x += r.x) >= mod) x -= mod;
        return *this;
    }
    constexpr mint& operator-=(const mint& r) noexcept {
        if ((x += mod - r.x) >= mod) x -= mod;
        return *this;
    }
    constexpr mint& operator*=(const mint& r) noexcept {
        x = static_cast<int>(1LL * x * r.x % mod);
        return *this;
    }
    constexpr mint& operator/=(const mint& r) noexcept {
        *this *= r.inv();
        return *this;
    }

    constexpr mint operator-() const noexcept { return mint(-x); }

    constexpr mint operator+(const mint& r) const noexcept {
        return mint(*this) += r;
    }
    constexpr mint operator-(const mint& r) const noexcept {
        return mint(*this) -= r;
    }
    constexpr mint operator*(const mint& r) const noexcept {
        return mint(*this) *= r;
    }
    constexpr mint operator/(const mint& r) const noexcept {
        return mint(*this) /= r;
    }

    constexpr bool operator==(const mint& r) const noexcept { return x == r.x; }
    constexpr bool operator!=(const mint& r) const noexcept { return x != r.x; }

    constexpr mint inv() const noexcept {
        int a = x, b = mod, u = 1, v = 0;
        while (b > 0) {
            int t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return mint(u);
    }

    constexpr mint pow(long long n) const noexcept {
        mint ret(1), mul(x);
        while (n > 0) {
            if (n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend std::ostream& operator<<(std::ostream& os, const mint& r) {
        return os << r.x;
    }

    friend std::istream& operator>>(std::istream& is, mint& r) {
        long long t;
        is >> t;
        r = mint(t);
        return is;
    }

   private:
    int x;
};
template <typename T>
class Matrix {
   public:
    static Matrix concat(const Matrix& A, const Matrix& B) {
        assert(A.m == B.m);
        Matrix C(A.m, A.n + B.n);
        for (int i = 0; i < A.m; ++i) {
            std::copy(A[i].begin(), A[i].end(), C[i].begin());
            std::copy(B[i].begin(), B[i].end(), C[i].begin() + A.n);
        }
        return C;
    }

    Matrix() = default;
    Matrix(int m, int n) : mat(m, std::vector<T>(n)), m(m), n(n) {}
    Matrix(std::initializer_list<std::initializer_list<T>> list) {
        for (auto& l : list) mat.emplace_back(l);
        m = mat.size();
        n = mat[0].size();
    }

    int row() const { return m; }
    int col() const { return n; }

    const std::vector<T>& operator[](int i) const { return mat[i]; }
    std::vector<T>& operator[](int i) { return mat[i]; }

    Matrix& operator+=(const Matrix& rhs) {
        assert(m == rhs.m && n == rhs.n);
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                mat[i][j] += rhs[i][j];
            }
        }
        return *this;
    }

    Matrix& operator-=(const Matrix& rhs) {
        assert(m == rhs.m && n == rhs.n);
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                mat[i][j] -= rhs[i][j];
            }
        }
        return *this;
    }

    Matrix operator+(const Matrix& rhs) const { return Matrix(*this) += rhs; }
    Matrix operator-(const Matrix& rhs) const { return Matrix(*this) -= rhs; }

    Matrix transpose() const {
        Matrix ret(n, m);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                ret[i][j] = mat[j][i];
            }
        }
        return ret;
    }

    Matrix matmul(const Matrix& B) const {
        assert(n == B.m);
        Matrix ret(m, B.n);
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < B.n; ++j) {
                for (int k = 0; k < n; ++k) {
                    ret[i][j] += mat[i][k] * B[k][j];
                }
            }
        }
        return ret;
    }

    Matrix rref() const {
        Matrix A(*this);
        int pivot = 0;
        for (int j = 0; j < n; ++j) {
            int i = pivot;
            while (i < m && eq(A[i][j], T(0))) ++i;
            if (i == m) continue;

            if (i != pivot) A[i].swap(A[pivot]);

            T p = A[pivot][j];
            for (int l = j; l < n; ++l) A[pivot][l] /= p;

            for (int k = 0; k < m; ++k) {
                if (k == pivot) continue;
                T v = A[k][j];
                for (int l = j; l < n; ++l) {
                    A[k][l] -= A[pivot][l] * v;
                }
            }

            ++pivot;
        }
        return A;
    }

    int rank() const {
        auto A = rref();
        for (int i = 0; i < m; ++i) {
            bool nonzero = false;
            for (int j = 0; j < n; ++j) {
                if (!eq(A[i][j], T(0))) {
                    nonzero = true;
                    break;
                }
            }
            if (!nonzero) return i;
        }
        return m;
    }

    template <typename U,
              typename std::enable_if<std::is_floating_point<U>::value>::type* =
                  nullptr>
    static constexpr bool eq(U a, U b) {
        return std::abs(a - b) < 1e-8;
    }

    template <typename U, typename std::enable_if<!std::is_floating_point<
                              U>::value>::type* = nullptr>
    static constexpr bool eq(U a, U b) {
        return a == b;
    }

   protected:
    std::vector<std::vector<T>> mat;
    int m, n;
};

template <typename T>
class SquareMatrix : public Matrix<T> {
    using Matrix<T>::Matrix;
    using Matrix<T>::eq;
    using Matrix<T>::n;

   public:
    static SquareMatrix I(int n) {
        SquareMatrix ret(n);
        for (int i = 0; i < n; ++i) ret[i][i] = 1;
        return ret;
    }

    SquareMatrix() = default;
    explicit SquareMatrix(int n) : Matrix<T>(n, n) {}
    SquareMatrix(const Matrix<T>& mat) : Matrix<T>(mat) {
        assert(Matrix<T>::m == n);
    }
    SquareMatrix(std::initializer_list<std::initializer_list<T>> list)
        : Matrix<T>(list) {
        assert(Matrix<T>::m == n);
    }

    SquareMatrix pow(long long k) const {
        auto ret = I(n);
        auto A(*this);
        while (k > 0) {
            if (k & 1) ret = ret.matmul(A);
            A = A.matmul(A);
            k >>= 1;
        }
        return ret;
    }

    T det() const {
        SquareMatrix A(*this);
        T ret = 1;
        for (int j = 0; j < n; ++j) {
            int i = j;
            while (i < n && eq(A[i][j], T(0))) ++i;
            if (i == n) return 0;

            if (i != j) {
                A[i].swap(A[j]);
                ret = -ret;
            }

            T p = A[j][j];
            ret *= p;
            for (int l = j; l < n; ++l) A[j][l] /= p;

            for (int k = j + 1; k < n; ++k) {
                T v = A[k][j];
                for (int l = j; l < n; ++l) {
                    A[k][l] -= A[j][l] * v;
                }
            }
        }
        return ret;
    }

    SquareMatrix inv() const {
        assert(!eq(det(), T(0)));
        auto IB = Matrix<T>::concat(*this, I(n)).rref();
        SquareMatrix B(n);
        for (int i = 0; i < n; ++i) {
            std::copy(IB[i].begin() + n, IB[i].end(), B[i].begin());
        }
        return B;
    }
};

using mint = Modint<(int)1e9 + 7>;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    const SquareMatrix<mint> mat = {{4, 1}, {1, 0}};

    auto get_index = [&](char d0, char d1) {
        if (d0 == '0' && d1 == '0') return 0;
        if (d0 == '1' && d1 == '0') return 1;
        if (d0 == '1' && d1 == '1') return 2;
        if (d0 == '0' && d1 == '1') return 3;
    };

    int T;
    cin >> T;
    while (T--) {
        string s, t;
        int k;
        cin >> s >> t >> k;
        if (s.size() > t.size()) swap(s, t);
        reverse(all(s));
        reverse(all(t));
        rep(i, 0, s.size()) {
            if (s[i] == '1') {
                t[i] = (t[i] == '0' ? '1' : '0');
            }
        }
        while (t.size() > 0 && t.back() == '0') {
            t.pop_back();
        }
        if (t.size() % 2 == 1) t.push_back('0');
        if (t.empty()) {
            auto res = mat.pow(k - 1);
            mint ans = res[0][0] + res[0][1] - 1;
            cout << ans << "\n";
        } else if (k > 2 || (k == 2 && t.substr(0, 2) != "00")) {
            cout << -1 << "\n";
        } else {
            mint ans = get_index(t[0], t[1]);
            if (k == 2) ans = 4;

            mint sz = 5;
            for (int d = 2; d < t.size(); d += 2) {
                ans += sz * get_index(t[d], t[d + 1]);
                sz = sz * 4 + 1;
            }
            cout << ans << "\n";
        }
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3692kb

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: 0ms
memory: 3596kb

input:

1
0 0 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3548kb

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: 0
Accepted
time: 0ms
memory: 3820kb

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:

295
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:

ok 100 numbers

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3900kb

input:

1000
1010011001 1100000000 1
1111001110 100100011 1
10000001 1110100110 1
1001000010 1111011110 1
11110001 101101110 1
10110001 110010 1
110111100 1111011111 1
1010101010 1111110000 1
11010110 11000110 1
1101101100 10001101 1
1101000110 111100110 3
1101100 10110 1
1001101001 10010001 1
1000110100 11...

output:

633
1267
752
627
629
257
1173
465
21
916
-1
145
1250
1006
155
783
412
684
400
1126
1204
185
298
932
535
246
1094
325
272
-1
-1
389
164
-1
-1
644
436
1271
261
741
351
212
985
426
236
1356
952
1256
1039
911
709
547
1349
142
229
1077
538
48
1089
378
1152
524
218
1161
485
884
751
299
206
268
95
933
769
...

result:

wrong answer 11th numbers differ - expected: '1361', found: '-1'