@codeforces - 715E@ Complete the Permutations


@description@

Given two permutation p, q, some of them replaced with 0 position.

Two p arrangement, the distance q is: a minimum of two elements perform switching operation in p, so that p, q are equal.

For each 0 <= k <= n, the required number of 0 and positive integers satisfying Alternatively Press p, q alternative method is still arranged, such that p, q distance to be k.

Stamp the original title here

@solution@

Consider how two arranged distance operator. If the p [i] -> q [ i] as a replacement, the replacement is required minimum number of exchanges sort.
It's a classic question, the answer is (n - the number of the replacement cycle contained).

Consider a special case: if the given p and q of a complete all-zero, the number of programs actually find the number n into nk cycles, i.e. the number of the first type Stirling.
So this question with more or less the number of first class Stirling collusion.

By p, q has a given number, we may now have to find out the cycle. The remaining will form a plurality of strands.
Take direct link to the existing number of classes for the first Stirling? Seemingly not. Consider an example: p = {1, 0} , q = {0, 2}, 1 and 2 is not possible at this time in the same cycle.

Visualize description, if p [i] = a and q [i] = 0, the following can be understood as a convex, or that a recessed below; Similarly, if there are p [j] = 0 and q [j ] = b, b can be interpreted as above convex, concave or that the above b.
If the a-> b should satisfy the above a and b below a recess be a protrusion.

In the middle of a chain transfer to chain things no limit, only two endpoints will have restrictions.
If a chain of the form a -> ... -> b, the irregularities of the above depends on this chain a, unevenness of the following depends b.
Thus limiting the strand may be described as irregularities of the vertical, we can be divided into four categories all chains.

In order to avoid discussions appear more categories, we assume that p [i] = 0, q [i] = i 0 is the vertical projection of all chains (understood to p [i] = n + i, q [i] = 0 and p [n + i] = 0, q [n + i] = n + i too).

The question to turn into 4 classes given number of chains, these chains find the number of programs is divided into p cycles.
Our idea of a return to the beginning: this title and first class Stirling number somewhat related. Thus analogy recursive formula for the first type number Stirling dp.

Stirling number of recursive formula: each addition a number, either as a single cycle, either connected to the back of a number of previously added.
Up and down the first recess is not seeking all the first type number of Stirling. But it may appear illegal situation: convex convex or concave concave contact phase.
Convex convex connecting directly to both the upper and lower concave as lubricants inserted to Au Au phase can not only be limited to the dp.
So let dp when the concave convex to the rear contact on the concave convex. And several other Stirling same.

The time complexity of seemingly is O (n ^ 2)?

@accepted code@

#include <cstdio>
const int MAXN = 250;
const int MOD = 998244353;
int f[MAXN + 5], g[MAXN + 5], c[4];
void solve() {
    int p = 0; f[0] = 1;
    for(int i=1;i<=c[0];i++) f[0] = 1LL*f[0]*i%MOD;
    for(int i=1;i<=c[3];i++) {
        for(int j=0;j<=p;j++) g[j] = f[j], f[j] = 0;
        p++;
        for(int j=1;j<=p;j++) f[j] = (1LL*g[j]*(i-1)%MOD + g[j-1]) % MOD;
    }
    for(int i=1;i<=c[2];i++) {
        for(int j=0;j<=p;j++) g[j] = f[j], f[j] = 0;
        p++;
        for(int j=1;j<=p;j++) f[j] = (1LL*g[j]*(c[3]+i-1)%MOD + g[j-1]) % MOD;
    }
    for(int i=1;i<=c[1];i++) {
        for(int j=0;j<=p;j++) g[j] = f[j], f[j] = 0;
        p++;
        for(int j=1;j<=p;j++) f[j] = (1LL*g[j]*(c[3]+i-1)%MOD + g[j-1]) % MOD;
    }
}
int a[MAXN + 5], b[MAXN + 5], n;
int u[MAXN + 5], d[MAXN + 5], p[MAXN + 5];
bool vis[MAXN + 5];
int main() {
    scanf("%d", &n);
    for(int i=1;i<=n;i++) scanf("%d", &a[i]), u[a[i]] = i;
    for(int i=1;i<=n;i++) scanf("%d", &b[i]), d[b[i]] = i;
    for(int i=1;i<=n;i++)
        if( u[i] ) p[i] = b[u[i]];
    for(int i=1;i<=n;i++)
        if( !d[i] || !a[d[i]] ) {
            int x = i;
            while( p[x] )
                vis[x] = true, x = p[x];
            vis[x] = true;
            if( !d[i] ) {
                if( !u[x] ) c[0]++;
                else c[1]++;
            }
            else {
                if( !u[x] ) c[2]++;
                else c[3]++;
            }
        }
    int cnt = 0;
    for(int i=1;i<=n;i++) {
        if( vis[i] ) continue;
        int x = i;
        do {
            vis[x] = true;
            x = p[x];
        }while( x != i );
        cnt++;
    }
    for(int i=1;i<=n;i++)
        if( !a[i] && !b[i] ) c[3]++;
    solve();
    for(int i=n;i>=cnt&&i>=1;i--)
        printf("%d ", f[i-cnt]);    
    for(int i=cnt-1;i>=1;i--) printf("%d ", 0);
}

@details@

This problem difficulty value 3400?Seemingly official to be fft practice?

Saying I do not know how I think so "image" of understanding.
Feel "image" to the somewhat abstract.

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/12020740.html