JZOJ5164 [6.25] NOIP2017 analog A little to do CF

topic

Description

Target rating over CLJ A little crazy to do recently in CF, CF saying this out of a quite wonderful pre-selection problems before the game ten minutes, requires ten minutes must work out this problem in order to participate in the CF.
This question is this, give you an N * N matrix, each row has a barrier, to ensure that any data not on the same line two obstacles, any two disorders are not the same column, asking you to put the pieces on N in this matrix (position obstacles can not put pieces), asking you to put the N pieces also meet every row only a pawn, pawn only a limit for each column, find how many kinds of programs.
God is all kinds of problems recently tortured a very tough battle, deranged A little bit is this super stuck to the water problem, but this is the best opportunity over the CLJ, so edgy that he found you, you take him CF successfully participate in the race now.

Input

A first line N, a next N * N matrix.

Output

The number of legal program.

Sample Input

2
0 1
1 0

Sample Output

1

Data Constraint

20% of the data to ensure that: N <= 10
60% of the data to ensure that: N <= 20 is
100% of the data to ensure that: N <= 200

answer

+ 20% blast search prune

+ 60% better burst search pruning deceive points (this test has not played this title person bins, so there is no way out of reach)

100% There are two ways, one is the original solution to a problem, one is the title of this test method people.

Act one: the wrong row of the formula (paste from Baidu Encyclopedia)

Recurrence formula

When \ (n-\) numbered elements in \ (n-\) a plurality of position numbers, each element number does not correspond with the position numbers Methods \ (D (n) \) represents, then \ (D (n-1 ) \) says \ (n-1 \) numbered elements in \ (n-1 \) numbered position, does not correspond to the number of ways of each other and so on.

The first step, the first \ (n-\) element in a position such as position \ (K \) , a total of \ (n-1 \) method;

The second step, put the number k elements, then there are two cases:

  1. Put it in position n, then, for the remaining \ (n-1 \) element, since the k-th element into the position \ (n-\) , the remaining \ (n-2 \) elements have \ (D (n-2) \) method;
  2. K-th element is not put it in the position \ (n-\) , this time, for these \ (n-1 \) element, there are \ (D (n-1) \) method;

In summary obtained

\(D(n) = (n-1) [D(n-2) + D(n-1)]\)

Specially, \ (D (. 1) = 0, D (2) =. 1 \) .

The following derivation of this term formulas by recursion relation:

For convenience, let \ (D (K) = K! N (K), K =. 1, 2, ..., n-\) ,

The \ (N (. 1) = 0, N (2) = 1/2 \) .

\ (n ≥ 3 \)时, \ (n! N (n) = (n-1) (n-1)! N (n-1) + (n-1)! N (n-2) \)

\ (Nn (t) = (n-1) o (n-1) + O (n-2) \)

Then there

\(N(n) - N(n-1) = - [N(n-1) - N(n-2)] / n = (-1/n) [-1/(n-1)] [-1/(n-2)]…(-1/3) [N(2) - N(1)] = (-1)^n / n!\)

therefore

\(N(n-1) - N(n-2) = (-1)^(n-1) / (n-1)!,\)$

\(N(2) - N(1) = (-1)^2 / 2!\).

Adding, available

\(N(n) = (-1)^2/2! + … + (-1)^(n-1) / (n-1)! + (-1)^n/n!\)

therefore

\(D(n) = n! [(-1)^2/2! + … + (-1)^(n-1)/(n-1)! + (-1)^n/n!]\).

Namely the wrong row formula.

(简化: \ (f [i] = (i-1) * (f [i-1] + f [i-2]) \) )

Inclusion-exclusion principle

With the inclusion-exclusion principle can also launch the wrong row of the formula:

A positive integer of 1, 2, 3, ......, n has a full array of n, where k k-th bit is arranged to have (n-1) types;!! Were taken as k 1, 2, 3, ......, n, the total of n * (n-1) are permutations of the at least one place, since the desire is for several Staggered, it should be subtracted arrangement;! but this time the two operands simultaneously good arrangement of a multi ruled out, should make up; when up on the same time there are a good number three rows are arranged to make up more than once, should be excluded; ......; continue this process, to get the number wrong kind of arrangement row for

\ (D (n) = n! - n! / 1! + N! / 2! - n! / 3! + ... + (-1) ^ n * n! / N! = Σ (k = 2 ~ n ) (-1) ^ k * n! / k! \) ,

That \ (D (n) = n ! [1/0 -! 1/1 + 1/2 -!!!! 1/3 + 1/4 + ... + (-1) ^ n / n!] \) .

Binomial inversion

We use the binomial inversion simpler to derive a general term formula.

Consider order imgrepresents imga number of programs put any figures, imgindicate imgnumbers do not count on the program on their location, easy to get by enumerating the number of digits is not on their location

img

Obtained by the binomial inversion

img

Notes that img, so we got his formula of general term, by imgand expand the number of combinations you can get simpler formula for the general term

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n, w[210][500], p[500], q[500], len = 1, l, k1;
void jia(int k)
{
    memset(q, 0, sizeof q);
    for (int i = 1; i <= len; i++)
        q[i] = w[k - 2][i] + w[k - 1][i];
    for (int i = 1; i <= len; i++)
    {
        q[i + 1] += q[i] / 10;
        q[i] %= 10;
    }
    while (q[len + 1]) len++;
}
void cheng(int k)
{
    jia(k);
    l = 0, k1 = k;
    memset(p, 0, sizeof p);
    while (k1 >= 10)
    {
        p[++l] = k1 % 10;
        k1 /= 10;
    }
    if (k1) p[++l] = k1;
    for (int i = 1; i <= len; i++)
        for (int j = 1; j <= l; j++)
            w[k][i + j - 1] += q[i] * p[j];
    for (int i = 1; i < len + l; i++)
    {
        w[k][i + 1] += w[k][i] / 10;
        w[k][i] %= 10;
    }
    len = len + l - 1;
    while (w[k][len + 1]) len++;
}
int main()
{
    memset(w, 0, sizeof w);
    scanf("%d", &n);
    w[1][1] = 1;
    w[2][1] = 2;
    for (int i = 3; i < n; i++) cheng(i);
    for (int i = len; i >= 1; i--) printf("%d", w[n - 1][i]);
    printf("\n");
    return 0;
}

Act II: people experience problems solution

Based on the model transformation can be found, the number of blank board for the program is fairly easy to find out, and with barriers checkerboard scheme can be achieved by the inclusion-exclusion principle (ie a total of minus put back together the pieces on the two pieces barriers on the barrier ......) preconditioning the number of combinations (discharge due to the selected position when the obstacle), the following equation can be derived with :( \ (n-\) line \ n-\) ( column, \ (m \) th disorders) \ ([Sigma Ans = (-1) * I * C IM (Ni)! \) ( \ (I \) enumerated disorders number from 0 to \ (m \) )

Guess you like

Origin www.cnblogs.com/featherZHY/p/11334062.html