Under construction

Still being built. Things may change

Lockpicking

Time limit1sMemory limit1024 MB

Summary
Build a keycard that keeps the error count below N from every unknown starting state of the lock, for up to 10^7 rounds.
Level

Hard9 of 10

Topics
Graph, DFS, Implementation
Solved
No attempts yet

Problem

The Hungarian State Treasury replaced the locking mechanism on one of their safes. This new lock can only be opened by custom keycards. The verification of a keycard follows a special protocol.

The lock has NN internal states numbered from 00 to N−1N - 1. For each ii from 00 to N−1N - 1, inclusive, there is a bit A[i]A[i], and there are two states S[i][0]S[i][0] and S[i][1]S[i][1] associated with state ii. A bit is a binary digit that is either 00 or 11. States S[i][0]S[i][0] and S[i][1]S[i][1] may coincide, and a state may be associated with itself.

Similarly, a keycard has MM states numbered from 00 to M−1M - 1. For each jj from 00 to M−1M - 1, bit B[j]B[j] and states T[j][0]T[j][0] and T[j][1]T[j][1] are associated with state jj. Hereafter, lock states and keycard states are both referred to as states.

In the verification process, the lock is paired with a keycard. Both of them can output bits and read the bits output by the other. At the start of the process, the lock is set to a specific initial state i0i_0. The keycard is also set to its own initial state j0j_0. They repeat the following steps at most 10710^7 times:

  1. Each of them outputs a single bit according to its current state. If the lock is in state ii, it outputs the bit A[i]A[i]. Analogously, the keycard outputs B[j]B[j] if it is in state jj.
  2. They read the bit that the other one output. The lock reads B[j]B[j], and the keycard reads A[i]A[i]. If the two bits differ, they register an error.
  3. The devices change state based on the bit they read and the states associated with the current one. The lock enters state S[i][B[j]]S[i][B[j]], and the keycard enters state T[j][A[i]]T[j][A[i]].

If the number of errors ever reaches KK, the verification fails and the process terminates. Otherwise, if they complete 10710^7 iterations without registering at least KK errors, the verification succeeds and the lock opens.

When the lock was installed, the mechanics forgot to specify the initial state i0i_0. As a result, whenever the lock is paired with a keycard, it starts in an arbitrary, unknown initial state.

Your task is to construct a keycard that can open the lock despite this mistake.

Constraints

  • 2≤N≤1502 \le N \le 150
  • 0≤A[i]≤10 \le A[i] \le 1 (for each ii such that 0≤i<N0 \le i < N)
  • 0≤S[i][0]<N0 \le S[i][0] < N and 0≤S[i][1]<N0 \le S[i][1] < N (for each ii such that 0≤i<N0 \le i < N)
  • 1≤M≤50 0001 \le M \le 50\,000
  • 0≤B[j]≤10 \le B[j] \le 1 (for each jj such that 0≤j<M0 \le j < M)
  • 0≤T[j][0]<M0 \le T[j][0] < M and 0≤T[j][1]<M0 \le T[j][1] < M (for each jj such that 0≤j<M0 \le j < M)
  • K=NK = N

Examples1

  1. Example 1

    Input
    2
    0 0 0
    1 1 1
    
    Expected output
    1
    0 0 0