#P8189. [USACO22FEB] Redistributing Gifts G

[USACO22FEB] Redistributing Gifts G

题目描述

Farmer John has NN gifts labeled 1N1…N for his NN cows, also labeled 1N1…N (1N18)(1≤N≤18). Each cow has a wishlist, which is a permutation of all NN gifts such that the cow prefers gifts that appear earlier in the list over gifts that appear later in the list.

FJ was lazy and just assigned gift ii to cow ii for all ii. Now, the cows have gathered amongst themselves and decided to reassign the gifts such that after reassignment, every cow ends up with the same gift as she did originally, or a gift that she prefers over the one she was originally assigned.

There is also an additional constraint: a gift may only be reassigned to a cow if it was originally assigned to a cow of the same type (each cow is either a Holstein or a Guernsey). Given QQ (1Qmin(105,2N))(1≤Q≤min(10^5,2^N)) length-NN breed strings, for each one count the number of reassignments that are consistent with it.

输入格式

The first line contains NN. The next NN lines each contain the preference list of a cow. It is guaranteed that each line forms a permutation of 1N1…N.

The next line contains QQ.

The final QQ lines each contain a breed string, each NN characters long and consisting only of the characters G and H. No breed string occurs more than once.

输出格式

For each breed string, print the number of reassignments that are consistent with it on a new line.

题目大意

Farmer John 给他的 N(1N18)N(1\leq N \leq 18) 头奶牛们准备了 NN 个礼物,每头奶牛有一个排列 aa,表示该奶牛对第 aia_i 个礼物的“喜好程度”为 ii

起初第 jj 头牛手上有第 jj 号礼物。

现有 Q(1Qmin(105,2N))Q(1\leq Q \leq min(10^5,2^N)) 次询问,每次给出仅含 'H' 或 'G' 的字符串,表示每头奶牛的品种,并询问在同品种奶牛可以互相交换礼物的情况下,满足任意一头牛对交换后礼物的“喜好程度”都小于等于其对原先礼物的“喜好程度”的交换方案数。

4
1 2 3 4
1 3 2 4
1 2 3 4
1 2 3 4
5
HHHH
HHGG
GHGH
HGGG
GHHG
2
1
1
2
2

提示

【样例解释】

In this example, for the first breed string, there are two possible reassignments:

  • The original assignment: cow 11 receives gift 11, cow 22 receives gift 22, cow 33 receives gift 33, and cow 44 receives gift 44.
  • Cow 11 receives gift 11, cow 22 receives gift 33, cow 33 receives gift 22, and cow 44 receives gift 44.

【数据范围】

  • For T=2,,13T=2,\cdots ,13, test case T satisfies N=T+4N=T+4.
  • Test cases 14-18 satisfy N=18N=18.