Topic: Given a length of 2^n arrangement, there are n operations, the first action is "to divide the sequence into 2^ (n-i+1) segments, each segment 2^ (i-1), and then choose Two-Segment Exchange", each operation at most once, to ask how many sequence of operations can be ordered out of the sequence
Orz Dzy
First, it is easy to see whether an operation sequence is legal and the sequence is irrelevant.
So we just need to make sure that every action in an action sequence is chosen, so the contribution of such sequence of actions to the answer is the factorial of the selected operand.
We grew up to the big Dfs, for the first I operation we divide the sequence into 2^ (n-i) segments, each length 2^i
We find that there is not a continuous increment in the sequence, and if such a segment is more than 2, it is obviously obsolete.
If there is no such segment, you do not need to perform this operation
If there is one such segment, determine whether the first and second halves of the segment are continuously incremented after swapping, and if so, then continue Dfs
If there are two such segments, determine the four exchange conditions and then the DFS
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M (1< <12) using namespace Std;int n;int a[m];long long fac[15],ans;void Swap (int a[],int b[],int len) {int i;for (I=0;i<len ; i++) swap (a[i],b[i]);} void DFS (int dpt,int cnt) {if (dpt==n) {Ans+=fac[cnt];return;} int Stack[3]={0,0,0},top=0;int i,j,temp=1<<dpt+1;for (i=0;i<1<<n;i+=temp) {if (a[i+ (temp>>1)-1 ]+1!=a[i+ (temp>>1)] {if (top==2) return; stack[++top]=i;}} if (top==0) {DFS (dpt+1,cnt); return;} if (top==1) {i=stack[1];if (A[i+temp-1]+1==a[i]) {Swap (a+i,a+i+ (temp>>1), temp>>1);D FS (dpt+1,cnt+1); Swap (a+i,a+i+ (temp>>1), temp>>1);} return;} if (top==2) {i=stack[1];j=stack[2];if (a[i+ (temp>>1) -1]+1==a[j+ (temp>>1)] && a[j+ (temp>>1 ) { -1]+1==a[i+ (temp>>1)]) {Swap (a+i,a+j,temp>>1);D FS (dpt+1,cnt+1); Swap (a+i,a+j,temp>>1); Swap (a+i+ (temp>>1), a+j+ (temp>>1), temp>>1);D FS (dpt+1,cnt+1); Swap (a+i+ (temp>>1), a+j+ (temp>>1), temp>>1);} if (a[j+ (temp>>1) -1]+1==a[i] && a[j+temp-1]+1==a[i+ (temp>>1)]) {Swap (a+i,a+j+ (temp>>1), TEMP>>1);D FS (dpt+1,cnt+1); Swap (a+i,a+j+ (temp>>1), temp>>1);} if (a[i+ (temp>>1) -1]+1==a[j] && a[i+temp-1]+1==a[j+ (temp>>1)]) {Swap (a+j,a+i+ (temp>>1), TEMP>>1);D FS (dpt+1,cnt+1); Swap (a+j,a+i+ (temp>>1), temp>>1);} return;}} int main () {int i;cin>>n;for (i=0;i<1<<n;i++) scanf ("%d", &a[i]); for (fac[0]=1,i=1;i<=n;i++) FAC [I]=fac[i-1]*i;dfs (0,0); Cout<<ans<<endl;return 0;}
Bzoj 3990 Sdoi2015 Sort DFS