[Counted] count exchange (CH3602) (composition count)

Problem

ACwing address topics

Luo Gu address topics

Classic question, good question. Also thanks Rose_max Gangster blog let me get to know this problem.


Solution 1

For a permutation \ (P_1, P_2, ..., P_n \) , each \ (P_i \) to \ (I \) is connected to a free edge constituting one of a plurality of simple rings undirected graph. I.e., the target state \ (n-\) a self-loop.

Lemma: The size of a \ (K \) simple ring becomes \ (K \) th from the ring, at least \ (k-1 \) views exchanged.

Use mathematical induction to prove that it is simple to prove omitted here.

Thus we can get a corollary: Suppose there FIG \ (K \) ring, then the minimum number of steps are switched \ (NK \) .


Let us consider the count.

Set \ (f [i] \) represents a size of \ (I \) ring, in the case of guaranteed minimum switching times, how many ways it becomes the target state.

Each exchange can size \ (I \) is split into a ring size \ (x, y \) of the two rings, \ (X + Y = n-\) . Set \ (T (x, y) \) indicates how many exchange process may be a size of \ (I \) rings are split into two size \ (x, y \) ring. Can be found: when the \ (x = y \) , the half of the method are repeated, \ (T (X, Y) = X \) ; otherwise, \ (T (X, Y) = X + Y \) .

The size of a \ (I \) is split into a ring size \ (x, y \) after two rings, \ (X \) cycloalkyl requires \ (x-1 \) times the exchange target state is reached, these \ (x-1 \) operations referred to as \ (x-1 \) a \ (0 \) , the same token \ (Y \) ring \ (y-1 \) operations referred to as \ (Y- 1 \) a \ (1 \) . Because (X \) \ ring (Y \) \ operation of interfering rings, both sides of the operation may be arranged randomly, and therefore, this is a full array of multiple sets .

In summary, it is possible to obtain \ (f [i] \) of the state transition equation:

\[f[i]=\sum_{x+y=i} f[x]*f[y]*T(x,y)*\frac{(i-2)!}{(x-1)!(y-1)!}\]

(Perhaps this formula can \ (FFT \) , if you can be \ (FFT \) , remember to tell me about the practice qwq)


Suppose arrangement \ (p_1, p_2, ..., p_n \) have \ (K \) sizes are \ (L_1, L_2, ..., L_k \) ring. Since this \ (K \) operation of interfering rings , the first of which can be written \ (I \) or more rings \ (L_i-1 \) operations as \ (L_i-1 \) a \ (I \) , full permutation operation is always a multiple set . the answer is:

\[Ans=(\prod_{i=1}^k f[L_i])*(\frac{(n-k)!}{\prod_{i=1}^k (L_i-1)!})\]

In \ (\ mod 10 ^ 9 + 9 \) the sense of the inverse of division can be obtained by solving Fermat's little theorem. Complexity with a \ (\ log \) .

Time complexity: \ (O (n-2 ^ \ n-log) \)

Seeking \ (F [\] \) array and a part of the code factorial (pretreatment)

void Init() {
    fc[0] = 1;  //阶乘 factorial
    for(int i=1;i<=1000;++i) fc[i] = fc[i-1] * i % mod;
    f[1] = 1;
    for(int i=2;i<=1000;++i) {
        for(int j=1;j<=i/2;++j) {   // (x,y)和(y,x)只能算一次 
            int inv = Pow(fc[i-j-1]*fc[j-1]%mod, mod-2);
            f[i] = (f[i] + f[i-j]*f[j]%mod*T(i-j,j)%mod*fc[i-2]%mod*inv%mod) % mod;
        }
    }
    for(int i=1;i<=10;++i) printf("%lld\n",f[i]);
}

Solution 2

Note that the above algorithm can not pass this question ( \ (n-\ leqslant. 5 ^ 10 \) ). So we come to play table before \ (10 \) key:

1 1 3 16 125 1296 16807 262144 4782969 100000000

Which is input into the \ (OEIS \) , or is sensitive to power the students can be found: \ (F [n-] = n-2-n-^ {} \) . With this law, we can solve this problem.

Time complexity: \ (O (n-\ log n-) \)

Code

Talk is cheap.Show me the code.

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
    int x=0,f=1; char ch=getchar();
    while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<3)+(x<<1)+(ch^48); ch=getchar(); }
    return x * f;
}
const int N = 1e5+7, mod = 1e9+9;
int n,cnt,ans;
int p[N],f[N],fc[N],L[N];
bool vis[N];
int Pow(int x,int y) {
    int res = 1, base = x;
    while(y) {
        if(y&1) res = res*base%mod; base = base*base%mod; y >>= 1;
    }
    return res;
}
void Init2() {
    fc[0] = 1;
    for(int i=1;i<N;++i) fc[i] = fc[i-1] * i % mod;
    f[1] = 1;
    for(int i=2;i<N;++i) f[i] = Pow(i,i-2);
}
int Dfs(int u) {
    vis[u] = 1;
    if(vis[p[u]]) return 1;
    return Dfs(p[u]) + 1;
}
void work() {
    cnt = 0;
    memset(vis, 0, sizeof(vis));
    n = read();
    for(int i=1;i<=n;++i) p[i] = read();
    for(int i=1;i<=n;++i)
        if(!vis[i]) L[++cnt] = Dfs(i);
    ans = fc[n-cnt] % mod;
    for(int i=1;i<=cnt;++i) {
        int inv = Pow(fc[L[i]-1], mod-2);
        ans = ans*f[L[i]]%mod*inv%mod;
    }
    printf("%lld\n",ans);
}
signed main()
{
    Init2();
    int T = read();
    while(T--) work();
    return 0;
}

Summary

A modulus wrong autistic noon, So we have to carefully anywhere it! !

This topic so I learned two classic models:

  • The arrangement \ (p_1, p_2, ..., p_n \) becomes the minimum switching frequency and increment sequence.

  • The arrangement \ (p_1, p_2, ..., p_n \) becomes increasing sequence, to ensure that a minimum number of times the program exchange

Also taught me to count some of the problems of skills / ideas / routines .

Guess you like

Origin www.cnblogs.com/BaseAI/p/12221703.html