This page is still under construction.

Parts of this page are still being built. What you see may change.

California Jones and the Gate to Freedom

Time limit1sMemory limit128 MB

Summary
Given n stones and a binary index b, decide whether the chosen n/2 stones are exactly the combination at lexicographic rank b among all size-n/2 subsets.
Level

Medium6 of 10

Topics
Combinatorics, Math, Implementation, Sorting
Solved
No attempts yet

Problem

California Jones (the sister of the famous Indiana Jones) is trapped in front of a huge gate and needs your help.

There are nn stones lying in a row, each marked with a distinct integer. In front of the gate there are exactly n/2n/2 holes, and Jones must place stones into them. Which hole a stone goes into does not matter; only which n/2n/2 stones are chosen matters.

The gate identifies one choice with a binary number. A binary number names a choice as follows:

  • Look at the stones by their position in the input: the 1st, 2nd, ..., nn-th stone.
  • Describe any choice of n/2n/2 stones by the ascending list of the positions it uses.
  • Sort all possible choices in increasing (lexicographic) order of these position lists. The choice using positions {1,2,…,n/2}\{1, 2, \dots, n/2\} comes first.
  • Number the sorted choices 0,1,2,…,(nn/2)−10, 1, 2, \dots, \binom{n}{n/2} - 1.

A binary string bb encodes a non-negative integer, and that integer is the index of a choice in this ordering.

Given a binary string bb and a set of n/2n/2 stones, decide whether that set is exactly the choice indexed by bb. If bb is not a valid index (that is, b≥(nn/2)b \ge \binom{n}{n/2}), it cannot match any set, so the answer is FALSE.

Input

The input contains several test cases. Each test case begins with the number of stones nn. The input is terminated by a line with n=0n = 0.

For every other test case, nn is even and 2≤n≤322 \le n \le 32. The next nn integers are the stone identifiers. The test case then gives kk, the number of queries. Each of the following kk queries consists of a binary string bb followed by n/2n/2 distinct integers naming the chosen stones. Every named stone is one of the nn stones, and the length of bb is at most 3030.

Output

For each query, print a single line containing TRUE if the chosen stones are exactly the choice identified by bb, and FALSE otherwise.

Examples1

  1. Example 1

    Input
    4
    12 50 74 34
    1
    00
    50 12
    
    8
    45 23 86 43 90 76 12 74
    2
    111001
    86 43 90 74
    010001
    45 86 43 90
    
    4
    12 50 74 34
    2
    101
    34 74
    110
    34 74
    
    0
    
    Expected output
    TRUE
    TRUE
    FALSE
    TRUE
    FALSE