The question ... Super detail ...
First, I guessed a conclusion. If there is a sort of scenario that is feasible, assume that this scenario is $S $.
Then we can rearrange the $S $ to a certain set of legitimate schemes.
So we can $O (2^N) $ enumeration for each operation into the not proceed, and then to judge, if feasible $ans $ + = $| s|! $
But how do you judge it?
We operate from small to large according to the type of operation.
Let's assume that we are now in the decision $i $ operation and do not need to proceed with the type number $< i$ before the operation is guaranteed.
Then we only consider those numbers where the positions are in the $2^i+1$ position.
We first find all the illegal locations, one of the following conditions is illegal location, set the current position is $pos $:
- Condition $1$: $A _{pos}$ cannot be represented as $a \times 2^i + 1$, where $a $ is a non-negative integer.
- Condition $2$: $A _{pos + 2^{i-1}} \ne A_{pos} + 2^{i-1}$.
No solution is met for one of the following three scenarios:
- If there are three or more illegal positions, then obviously there is no solution.
- If there is no illegal location, but the current state must be one operation, then no solution.
- If there is an illegal location, but the current state must not be manipulated, then no solution.
So let's start with the discussion:
When there is no illegal position, the direct recursive solution.
When there is only one illegal position, the Exchange $A _{pos}$ and $A _{pos + 2^{i-1}}$, recursive solution.
Here should have exchanged a range of, but useful actually only $A _{pos}$ and $A _{pos + 2^{i-1}}$ This two number.
Because the other numbers we do not consider now, but also not to consider, anyway, the guarantee is legal, so we only exchange these two numbers.
When there are two illegal locations, set two illegal positions for $pos _1$ and $pos _2$
We divide the illegal position into two categories: the first class satisfies the condition $1$, and the second class is not satisfied with the condition $1$ but satisfies the condition $2$.
- $type (pos_1) = 2$ && $type (pos_2) = 2$: There can be two possible legal interchanges: interchange $A _{pos_1}$ and $A _{pos_2}$ or swap $A _{pos_1 + 2^{i-1}}$ and $A _{pos_2 + 2^{i-1}}$.
- $type (pos_1) = 2$ && $type (pos_2) = 1$: There is only one possible legal interchange: Exchange $A _{pos_1 + 2^{i-1}}$ and $A _{pos_2}$.
- $type (pos_1) = 1$ && $type (pos_2) = 2$: There is only one possible legal interchange: Interchange $A _{pos_1}$ and $A _{pos_2 + 2^{i-1}}$.
- $type (pos_1) = 1$ && $type (pos_2) = 1$: no solution.
Then the recursive solution can be. Notice that when the exchange is done, we should judge whether the original illegal position has become lawful, and if it is illegal, it will be awarded directly.
Determine the complexity of each state:
$$\sum_{i=1}^{n}2^i\times\frac{2^n}{2^i} = \sum_{i=1}^{n}2^n = N\times 2^n$$
So the complexity of the whole problem is: $O (n2^{2n}) $.
However, the constant is very small, it should be difficult to construct a group of cards to the upper bound of the data, so the run is relatively fast ...
1 2#include <cmath>3#include <cstdio>4#include <cstring>5#include <iostream>6#include <algorithm>7 using namespacestd;8typedefLong LongLL;9 #defineN 12 + 5Ten #defineSIZE 1 << 12 | 5 One A intN; - LL ans; - LL Fac[n]; the intA[size], cnt[size]; - -InlineBOOLDfsintStateintlen) - { + if(Len >> (n +1))return 1; - intCNT =0, Pos_1, pos_2; + for(inti =0; I < (1<< N); i + =len) A { at int_i = i + (len >>1); - if(A[i] & (Len-1)) || A[_i]! = A[i] + (len >>1)) - { -CNT + +; - if(CNT = =1) Pos_1 =i; - Else if(CNT = =2) Pos_2 =i; in Else return 0; - } to } + if(State &1) ==0&& CNT)return 0; - if(State &1) &&!cnt)return 0; the if(!cnt)returnDFS (state >>1, Len <<1); * if(CNT = =1) $ {Panax NotoginsengSwap (A[pos_1], A[pos_1 + (len >>1)]); - BOOLOK = DFS (state >>1, Len <<1); theSwap (A[pos_1], A[pos_1 + (len >>1)]); + returnOK; A } the if(CNT = =2) + { - BOOLOK =1; $ if(A[pos_1] & (Len-1)) && (A[pos_2] & (Len-1)))return 0; $ Else if(A[pos_1] & (Len-1)) - { -Swap (A[pos_1], a[pos_2 + (len >>1)]); the if(A[pos_1] + (len >>1)! = A[pos_1 + (len >>1)]) OK =0; - if(A[pos_2] + (len >>1)! = a[pos_2 + (len >>1)]) OK =0;Wuyi if(ok) OK = DFS (state >>1, Len <<1); theSwap (A[pos_1], a[pos_2 + (len >>1)]); - returnOK; Wu } - Else if(A[pos_2] & (Len-1)) About { $Swap (a[pos_2], A[pos_1 + (len >>1)]); - if(A[pos_1] + (len >>1)! = A[pos_1 + (len >>1)]) OK =0; - if(A[pos_2] + (len >>1)! = a[pos_2 + (len >>1)]) OK =0; - if(ok) OK = DFS (state >>1, Len <<1); ASwap (a[pos_2], A[pos_1 + (len >>1)]); + returnOK; the } - Else $ { the swap (A[pos_1], a[pos_2]); the if(A[pos_1] + (len >>1)! = A[pos_1 + (len >>1)]) OK =0; the if(A[pos_2] + (len >>1)! = a[pos_2 + (len >>1)]) OK =0; the if(ok) OK = DFS (state >>1, Len <<1); - swap (A[pos_1], a[pos_2]); in if(OK)return 1; the theOK =1; AboutSwap (A[pos_1 + (len >>1)], a[pos_2 + (len >>1)]); the if(A[pos_1] + (len >>1)! = A[pos_1 + (len >>1)]) OK =0; the if(A[pos_2] + (len >>1)! = a[pos_2 + (len >>1)]) OK =0; the if(ok) OK = DFS (state >>1, Len <<1); +Swap (A[pos_1 + (len >>1)], a[pos_2 + (len >>1)]); - returnOK; the }Bayi } the } the - intMain () - { the #ifndef Online_judge theFreopen ("3990.in","R", stdin); theFreopen ("3990.out","W", stdout); the #endif - thescanf"%d", &n); thefac[0] =1; the for(inti =1; I <= N; i + +)94Fac[i] = fac[i-1] *i; the for(inti =1; I <= (1<< N); i + +) the { thescanf"%d", A + i-1);98A[i-1] --; AboutCnt[i] = cnt[i-(I & i)] +1; - }101 for(ints =0; S < (1<< N); S + +)102 if(Dfs (S,2)) ans + =Fac[cnt[s]];103printf"%lld\n", ans);104 the #ifndef Online_judge106 fclose (stdin);107 fclose (stdout);108 #endif109 return 0; the}
3990_gromah
BZOJ 3990 [sdoi 2015] sorting problem solving report