Submission #428573


Source Code Expand

#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>
#include <bitset>
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

/**
 * 行列ライブラリ with Mod
 */
template<int N, int M>
struct MatrixMod2 {
    array<bitset<M>, N> d;

    MatrixMod2() {
        for (int i = 0; i < N; i++) {
            d[i].reset();
        }
    }

    bitset<M>& operator[](int p) {
        return d[p];
    }
    
    const bitset<M>& operator[](int p) const {
        return d[p];
    }
    
    MatrixMod2& operator=(const MatrixMod2 &other) {
        d = other.d;
        return *this;
    }

    MatrixMod2 operator+(const MatrixMod2 &right) {
        MatrixMod2 res(N, M);
        for (int i = 0; i < N; i++) {
            res[i] = d[i]^right[i];
        }
        return res;
    }
    
    template<int K>
    MatrixMod2<N, K> operator*(const MatrixMod2<M, K> &right) {
        MatrixMod2<N, K> res;
        MatrixMod2<K, M> r;
        for (int i = 0; i < K; i++) {
            for (int j = 0; j < M; j++) {
                r[i][j] = right[j][i]; 
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < K; j++) {
                res[i][j] = (d[i]&r[j]).count() % 2;
            }
        }
        return res;
    }

    template<int K>
    MatrixMod2<N, K> trans_mul(const MatrixMod2<K, M> &r) {
        MatrixMod2<N, K> res;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < K; j++) {
                res[i][j] = (d[i]&r[j]).count() % 2;
            }
        }
        return res;
    }

    MatrixMod2 pow(ll p) {
        assert(N == M);
        MatrixMod2<N, M> res, buf = *this;
        for (int i = 0; i < N; i++) res[i][i] = true;
        while(p != 0) {
            if (p % 2) {
                res = res*buf;
            }
            p /= 2;
            buf = buf*buf;
        }
        return res;
    }

    MatrixMod2<M, N> transposed() {
        MatrixMod2<M, N> res;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                res[j][i] = d[i][j];
            }
        }
        return res;
    }
};

ull a, b, x;

bool solve1() {
    ull aa = a;
    unordered_set<ull> s;
    for (int i = 0; i < (1<<19); i++) {
        if (s.count(aa)) {
            printf("-1\n");
            return true;
        }
        s.insert(aa);
        if (aa == x) {
            printf("%d\n", i);
            return true;
        }
        aa = (aa/2) ^ (aa % 2 * b);
    }
    return false;
}

MatrixMod2<40, 40> base, bigB;


MatrixMod2<1, 40> m;

ull next(ull aa) {
    m[0] = bitset<40>(aa);
    return (m.trans_mul(bigB))[0].to_ullong();
}


MatrixMod2<40, 40> powt[60];

ull succ(ull aa, ll t) {
    m[0] = bitset<40>(aa);
    int i = 0;
    while (t) {
        if (t & 1) {
            m = m.trans_mul(powt[i]);
        }
        i++;
        t /= 2;
    }
    return m[0].to_ullong();
}

int main() {
    cin >> a >> b >> x;

    if (solve1()) return 0;

    for (int i = 0; i < 39; i++) {
        base[i+1][i] = true;
    }

    base[0] = bitset<40>(b);

    bigB = base.pow(1<<18).transposed();

    for (int i = 0; i < 50; i++) {
        powt[i] = base.pow(1ULL<<i).transposed();
    }

    unordered_map<ull, int> mp;
    ull xx = x;
    for (int i = 0; i < (1<<18); i++) {
        mp[xx] = i;
        xx = (xx/2) ^ (xx % 2 * b);
    }


    ull aa = a;
    for (ll i = 0; i <= (1<<18); i++) {
        if (i && mp.count(aa)) {
            if (succ(a, i * (1<<18) - mp[aa]) == x) {
                printf("%lld\n", i * (1<<18) - mp[aa]);
                return 0;
            }
        }
        aa = next(aa);
    }
    printf("-1\n");
    return 0;
}

Submission Info

Submission Time
Task K - 乱数調整
User yosupo
Language C++11 (GCC 4.9.2)
Score 300
Code Size 4123 Byte
Status AC
Exec Time 836 ms
Memory 29592 KB

Judge Result

Set Name Subtask All
Score / Max Score 30 / 30 270 / 270
Status
AC × 20
AC × 63
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 27 ms 928 KB
scrambled_01.txt AC 26 ms 1056 KB
scrambled_02.txt AC 27 ms 984 KB
scrambled_03.txt AC 25 ms 1052 KB
scrambled_04.txt AC 51 ms 4320 KB
scrambled_05.txt AC 28 ms 1184 KB
scrambled_06.txt AC 33 ms 2008 KB
scrambled_07.txt AC 42 ms 3172 KB
scrambled_08.txt AC 92 ms 9704 KB
scrambled_09.txt AC 92 ms 9448 KB
scrambled_10.txt AC 78 ms 7872 KB
scrambled_11.txt AC 45 ms 3296 KB
scrambled_12.txt AC 52 ms 4668 KB
scrambled_13.txt AC 33 ms 1748 KB
scrambled_14.txt AC 690 ms 29460 KB
scrambled_15.txt AC 714 ms 29520 KB
scrambled_16.txt AC 836 ms 29520 KB
scrambled_17.txt AC 794 ms 29592 KB
scrambled_18.txt AC 527 ms 29468 KB
scrambled_19.txt AC 527 ms 29588 KB
scrambled_20.txt AC 530 ms 29472 KB
scrambled_21.txt AC 538 ms 29520 KB
scrambled_22.txt AC 519 ms 29516 KB
scrambled_23.txt AC 532 ms 29516 KB
scrambled_24.txt AC 630 ms 29512 KB
scrambled_25.txt AC 501 ms 29588 KB
scrambled_26.txt AC 575 ms 29592 KB
scrambled_27.txt AC 592 ms 29516 KB
scrambled_28.txt AC 27 ms 860 KB
scrambled_29.txt AC 25 ms 1000 KB
scrambled_30.txt AC 27 ms 988 KB
scrambled_31.txt AC 27 ms 1052 KB
scrambled_32.txt AC 28 ms 1048 KB
scrambled_33.txt AC 27 ms 1052 KB
scrambled_34.txt AC 46 ms 4328 KB
scrambled_35.txt AC 53 ms 4992 KB
scrambled_36.txt AC 86 ms 9244 KB
scrambled_37.txt AC 174 ms 17488 KB
scrambled_38.txt AC 429 ms 29516 KB
scrambled_39.txt AC 798 ms 29520 KB
scrambled_40.txt AC 518 ms 29516 KB
scrambled_41.txt AC 706 ms 29516 KB
scrambled_42.txt AC 596 ms 29520 KB
subtask_00.txt AC 27 ms 932 KB
subtask_01.txt AC 28 ms 932 KB
subtask_02.txt AC 26 ms 988 KB
subtask_03.txt AC 26 ms 1056 KB
subtask_04.txt AC 29 ms 1044 KB
subtask_05.txt AC 28 ms 932 KB
subtask_06.txt AC 28 ms 1048 KB
subtask_07.txt AC 27 ms 916 KB
subtask_08.txt AC 28 ms 1060 KB
subtask_09.txt AC 26 ms 1048 KB
subtask_10.txt AC 27 ms 1040 KB
subtask_11.txt AC 28 ms 932 KB
subtask_12.txt AC 27 ms 932 KB
subtask_13.txt AC 28 ms 1048 KB
subtask_14.txt AC 27 ms 928 KB
subtask_15.txt AC 27 ms 1040 KB
subtask_16.txt AC 27 ms 1048 KB
subtask_17.txt AC 28 ms 1056 KB
subtask_18.txt AC 27 ms 924 KB
subtask_19.txt AC 25 ms 1052 KB