Submission #2331330


Source Code Expand

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ull = unsigned long long;
using ll = long long;
using ld = long double;
constexpr ll MOD = 1000000007LL;
template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;
template <typename Functor>
struct fix_type
{
    Functor functor;
    template <typename... Args>
    decltype(auto) operator()(Args&&... args) const& { return functor(functor, std::forward<Args>(args)...); }
};
template <typename Functor>
fix_type<typename std::decay<Functor>::type> fix(Functor&& functor) { return {std::forward<Functor>(functor)}; }

template <int N>
using BitVector = bitset<N>;  // [a_(N-1),a_(N-2),...,a_1,a_0] であることに注意
template <int N>
using BitMatrix = vector<BitVector<N>>;
// m_0   = [m_0(N-1)    ,m_0(N-2)    , ... ,m_01    ,m_00]
// m_1   = [m_1(N-1)    ,m_1(N-2)    , ... ,m_11    ,m_10]
// ...
// m_N-1 = [m_(N-1)(N-1),m_(N-1)(N-2), ... ,m_(N-1)1,m_(N-1)0] であることに注意

template <int N>
BitMatrix<N> transposed(const BitMatrix<N>& mat)
{
    assert(mat.size() == N);
    BitMatrix<N> ans(N);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) { ans[i][j] = mat[N - 1 - j][N - 1 - i]; }
    }
    return ans;
}

template <int N>
BitMatrix<N> mul(const BitMatrix<N>& a, const BitMatrix<N>& b)
{
    assert(a.size() == N);
    assert(b.size() == N);
    const auto bt = transposed<N>(b);
    BitMatrix<N> ans(N);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) { ans[i][N - 1 - j] = (a[i] & bt[j]).count() % 2; }
    }
    return ans;
}

template <int N>
BitVector<N> mul(const BitMatrix<N>& m, const BitVector<N>& v)
{
    assert(m.size() == N);
    BitVector<N> ans(N);
    for (int i = 0; i < N; i++) { ans[N - 1 - i] = (m[i] & v).count() % 2; }
    return ans;
}

template <int N>
static BitMatrix<N> UnitBitMatrix()
{
    BitMatrix<N> ans(N);
    for (int i = 0; i < N; i++) { ans[i][N - 1 - i] = 1; }
    return ans;
}

template <int N>
BitMatrix<N> power(const BitMatrix<N>& mat, const ll n)
{
    if (n == 0) { return UnitBitMatrix<N>(); }
    if (n % 2 == 1) {
        return mul<N>(power<N>(mat, n - 1), mat);
    } else {
        const auto pp = power<N>(mat, n / 2);
        return mul<N>(pp, pp);
    }
}

int main()
{
    constexpr int N = 36;
    ull A, B, X;
    cin >> A >> B >> X;
    const BitVector<N> a(A);
    const BitVector<N> x(X);
    BitMatrix<N> mat(N);
    for (int i = 1; i < N; i++) { mat[i][N - i] = 1; }
    for (int i = 0; i < N; i++) { mat[N - 1 - i][0] = (B >> i) & 1; }
    constexpr ll SQRT = 1LL << 18;
    const BitMatrix<N> MS = power<N>(mat, SQRT);
    map<ull, ll> mp;
    BitVector<N> av = mul<N>(MS, a);
    for (ll i = 1; i <= SQRT; i++) {
        const ull v = av.to_ullong();
        if (mp.find(v) == mp.end()) { mp[v] = i * SQRT; }
        av = mul<N>(MS, av);
    }
    BitVector<N> xv = x;
    ll ans = INF<ll>;
    for (ll i = 0; i < SQRT; i++) {
        const ull v = xv.to_ullong();
        if (mp.find(v) != mp.end() and mp[v] >= i) { ans = min(ans, mp[v] - i); }
        xv = mul<N>(mat, xv);
    }
    if (ans == INF<ll>) {
        cout << -1 << endl;
    } else {
        if (mul<N>(power<N>(mat, ans), a).to_ullong() == x.to_ullong()) {
            cout << ans << endl;
        } else {
            cout << -1 << endl;
        }
    }
    return 0;
}

Submission Info

Submission Time
Task K - 乱数調整
User pachicobue
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3505 Byte
Status WA
Exec Time 410 ms
Memory 18688 KB

Judge Result

Set Name Subtask All
Score / Max Score 0 / 30 0 / 270
Status
AC × 16
WA × 4
AC × 55
WA × 8
Set Name Test Cases
Subtask subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt, subtask_17.txt, subtask_18.txt, subtask_19.txt
All scrambled_00.txt, scrambled_01.txt, scrambled_02.txt, scrambled_03.txt, scrambled_04.txt, scrambled_05.txt, scrambled_06.txt, scrambled_07.txt, scrambled_08.txt, scrambled_09.txt, scrambled_10.txt, scrambled_11.txt, scrambled_12.txt, scrambled_13.txt, scrambled_14.txt, scrambled_15.txt, scrambled_16.txt, scrambled_17.txt, scrambled_18.txt, scrambled_19.txt, scrambled_20.txt, scrambled_21.txt, scrambled_22.txt, scrambled_23.txt, scrambled_24.txt, scrambled_25.txt, scrambled_26.txt, scrambled_27.txt, scrambled_28.txt, scrambled_29.txt, scrambled_30.txt, scrambled_31.txt, scrambled_32.txt, scrambled_33.txt, scrambled_34.txt, scrambled_35.txt, scrambled_36.txt, scrambled_37.txt, scrambled_38.txt, scrambled_39.txt, scrambled_40.txt, scrambled_41.txt, scrambled_42.txt, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt, subtask_17.txt, subtask_18.txt, subtask_19.txt
Case Name Status Exec Time Memory
scrambled_00.txt AC 90 ms 256 KB
scrambled_01.txt AC 91 ms 256 KB
scrambled_02.txt AC 87 ms 256 KB
scrambled_03.txt AC 84 ms 256 KB
scrambled_04.txt AC 315 ms 8448 KB
scrambled_05.txt AC 196 ms 640 KB
scrambled_06.txt AC 246 ms 2176 KB
scrambled_07.txt AC 310 ms 6400 KB
scrambled_08.txt AC 384 ms 16128 KB
scrambled_09.txt AC 356 ms 16640 KB
scrambled_10.txt AC 410 ms 16128 KB
scrambled_11.txt AC 409 ms 16128 KB
scrambled_12.txt AC 329 ms 8320 KB
scrambled_13.txt AC 248 ms 2304 KB
scrambled_14.txt AC 372 ms 16640 KB
scrambled_15.txt AC 384 ms 16640 KB
scrambled_16.txt AC 377 ms 16640 KB
scrambled_17.txt AC 377 ms 16640 KB
scrambled_18.txt AC 376 ms 16640 KB
scrambled_19.txt AC 374 ms 16640 KB
scrambled_20.txt AC 398 ms 16640 KB
scrambled_21.txt AC 386 ms 16640 KB
scrambled_22.txt AC 396 ms 16640 KB
scrambled_23.txt AC 394 ms 16640 KB
scrambled_24.txt AC 393 ms 16640 KB
scrambled_25.txt AC 384 ms 16640 KB
scrambled_26.txt AC 397 ms 16640 KB
scrambled_27.txt AC 390 ms 16640 KB
scrambled_28.txt WA 77 ms 256 KB
scrambled_29.txt AC 77 ms 256 KB
scrambled_30.txt WA 77 ms 256 KB
scrambled_31.txt AC 93 ms 256 KB
scrambled_32.txt WA 77 ms 256 KB
scrambled_33.txt WA 85 ms 256 KB
scrambled_34.txt AC 282 ms 4352 KB
scrambled_35.txt AC 313 ms 8448 KB
scrambled_36.txt AC 353 ms 16640 KB
scrambled_37.txt AC 356 ms 16640 KB
scrambled_38.txt AC 348 ms 16640 KB
scrambled_39.txt AC 383 ms 18688 KB
scrambled_40.txt AC 380 ms 16640 KB
scrambled_41.txt AC 373 ms 16640 KB
scrambled_42.txt AC 377 ms 16640 KB
subtask_00.txt AC 93 ms 256 KB
subtask_01.txt AC 109 ms 256 KB
subtask_02.txt AC 135 ms 256 KB
subtask_03.txt AC 135 ms 256 KB
subtask_04.txt AC 156 ms 256 KB
subtask_05.txt AC 112 ms 256 KB
subtask_06.txt AC 156 ms 256 KB
subtask_07.txt AC 111 ms 256 KB
subtask_08.txt AC 156 ms 256 KB
subtask_09.txt AC 121 ms 256 KB
subtask_10.txt AC 104 ms 256 KB
subtask_11.txt AC 141 ms 256 KB
subtask_12.txt AC 111 ms 256 KB
subtask_13.txt AC 107 ms 256 KB
subtask_14.txt WA 77 ms 256 KB
subtask_15.txt AC 77 ms 256 KB
subtask_16.txt WA 77 ms 256 KB
subtask_17.txt AC 79 ms 256 KB
subtask_18.txt WA 77 ms 256 KB
subtask_19.txt WA 85 ms 256 KB