# Problem Link:

Contest# Difficulty:

Hard# Pre-Requisites:

dynamic-programming, inclusion-exclusion, combinatorics, bipartite-graphs# Problem:

Given **3 * N** matrix, whose first two rows are filled with permutations of **[1..N]**, and third row is empty, find the number of ways of filling the third row such that each number(between 1 to N) appears at most once in each row and each column. The part of matrix already filled satisfies this property.

# Explaination:

The current problem can be translated to finding the number of perfect matchings in a given (N-2)-regular N*N bipartite graph.

The vertices on left hand side of bipartite-graph represent positions in the third row. The vertices on the right hand side represent colors that can be filled into some position. The edges are assignments compatible with the first two rows (i.e. if first row is array **A*** and second row is array **B***, then there is an edge from **i** to all colors except **A*** and **B***).

Coloring the third row of our matrix is equivalent to choosing exactly one edge(from the above graph **G**) incident to each position. The manner in which we have put edges ensure that **C*** ∉ **{A*, B*}**. To make sure that **C* ≠ C[j]**, the edges chosen should form a perfect matching.

Therefore, we need to find number of perfect matchings in a given (N-2)-regular bipartite graph **G**.
Let me define another related graph **G'** which has same set of vertices as **G**, and all left to right edges that are *not* in **G** (i.e. vertex **i** on LHS has edge to vertices **A*** and **B*** on RHS).

The first step towards solution is realizing that the graph **G'** has very simple structure. Every vertex has degree *2*, so it is a union of disjoint cycles. If suppose we were asked to count number of matchings in **G'**, it would be far simpler. Given the size of all cycles of **G'**, the number of k-matchings can be found with a simple DP. Let **Φ(k, G')** denote number of **k**-matchings in **G'**.

Let **F(k)** = The number of perfect matchings in **G U G'** such that exactly **k** edges belong to **G'** and **N-k** edges belong to **G**.

Any such matching consists of a k-matching in **G'**. So, can we use the value of **Φ(k, G')** ?

The only problem in using **Φ(k, G')** is that we dont know the number of ways to match the remaining vertices only using edges from **G**. To overcome this, we will consider all matchings of the remaining vertices in **G U G'** (which is **(N-k)!** in number), and then remove those where more than **k** edges are used from **G'**.

F(k) = Φ(k, G') * (N-k)! - F(k+1) * (k+1 choose k) - F(k+2) * (k+2 choose k) .... - F(N) * (N choose k)--(1)

**Actual program for this is**:

#include #include #include #includeusing namespace std;

#define MEM(a,b) memset(a,(b),sizeof(a))

#define MAX(a,b) ((a) > (b) ? (a) : (b))

#define MIN(a,b) ((a) < (b) ? (a) : (b))#define maxn 1000

#define maxt 10

#define M 1000000007#define MID 0

#define START 1

#define END 2typedef long long LL;

int A[1005],B[1005];

int posA[1005],posB[1005];

int n;

int seen[1005];

int type[1005];

int dp[1005];

int memo[1005][1005][2][2];

int ncr[1005][1005];

int fact[1005];void init()

{

int i,j;`fact[0]=1; for(i=1;i<=maxn;i++) fact*=((LL)fact[i-1]*i)%M; for(i=0;i<=maxn;i++) ncr*[0]=1; for(i=1;i<=maxn;i++) for(j=1;j<=i;j++) ncr*[j]=(ncr[i-1][j]+ncr[i-1][j-1])%M;`

}

/*

pos = The current number

k = number of forbidden pairs remaining to match

a = true if the position at the right is matched with the previous number

b = true if the first number in the current cycle is placed at the last position

*/int solve(int pos,int k,int a,int b)

{

LL ret=0;`if(pos==n) return (k==0); if(k<0 ) return 0; if(memo[pos][k][a]**!=-1) return memo[pos][k][a]**; if(type[pos]==START) { ret+=solve(pos+1,k-1,0,1); ret+=solve(pos+1,k-1,1,0); ret+=solve(pos+1,k,0,0); } if(type[pos]==END ) { if(!a) ret+=solve(pos+1,k-1,0,0); if(!b) ret+=solve(pos+1,k-1,0,0); ret+=solve(pos+1,k,0,0); } if(type[pos]==MID) { if(!a) ret+=solve(pos+1,k-1,0,b); ret+=solve(pos+1,k-1,1,b); ret+=solve(pos+1,k,0,b); } while(ret>=M) ret-=M; return memo[pos][k][a]**=ret;`

}

int main()

{

int i,j,k,T,cs=0;`init(); scanf("%d",&T); assert(T>=1 && T<=maxt); while(T--) { MEM(seen,0); MEM(type,0); MEM(memo,-1); scanf("%d",&n); assert(n>=3 && n<=maxn); for(i=0;i<n;i++) scanf("%d",&A*); for(i=0;i<n;i++) scanf("%d",&B*); MEM(posA,-1);MEM(posB,-1); for(i=0;i<n;i++) { assert(A*!=B*); assert(A*>=1 && A*<=n && posA[A*-1]==-1); assert(B*>=1 && B*<=n && posB[B*-1]==-1); --A*;--B*; posA[A*]=i; posB[B*]=i; } // Finds the permutation cycles and places the numbers of the same cycle consecutively int cnt=0; for(i=0;i<n;i++) { if(seen*) continue; int u=i; type[cnt]=START; while(1) { cnt++; int x= B[posA]; u=B[posA]; if(u==i) break; seen=1; } type[cnt-1]=END; } /* Number of ways k distinct forbidden number-position pairs can be chosen And use inclusion-exclusion principle to compute the number of permutations with exactly 0 forbidden positions */ for(k=n;k>=0;k--) { LL q=solve(0,k,0,0); q=(q*fact[n-k])%M; for(i=k+1;i<=n;i++) q=(q-((LL)ncr*[k]*dp*)%M)%M; dp[k]=q; } printf("%d`

",(dp[0]+M)%M);

}

return 0;

}