jzoj 3424. [simulation] painters NOIP2013

Description

Hector is a burly painters, and very like thinking = =

Now, there are N temple pillars arranged in a straight line, from 1 to N label, required by the elders of these pillars will paint fresh coat of paint. Hector tub has K different paint color, the paint can just paint bucket i Ci pillars, and, C1 + C2 + C3 ... CK = N (i.e., N painting pillars just run all the paint). Elders to spite Hector, requires not the same color of the adjacent columns.

Hector like to think not only did not immediately began to paint, but began pondering some strange questions, such as how many paint programs, a total of?

In order to begin to paint as soon as Hector, you tell him the answer as soon as possible.

Input

The first line of a positive integer T, represents a set of test data

For each set of test data:

Line 1: a positive integer K

Line 2: K positive integer, i is the number of columns of the paint bucket can be painted, Ci.

Output

For each set of input data, output line an integer representing the number of programs stucco mod 1000000007.

Sample Input

3
3
1 2 3
5
2 2 2 2 2
10
1 1 2 2 3 3 4 4 5 5

Sample Output

10
39480
85937576

Data Constraint

30% N≤10, T≤5
50% N≤15, T≤5
80% K≤15,Ci≤5,T≤500
100% K≤15,Ci≤6,T≤2000

Solution

Initially I thought it was a conclusion problem, did not think that was DP.
We set \ (F [i] [j] \) represents the i-th color brush, this embodiment has the same number of the j-th column neighboring color.
Note brush to the number of color blocks is now as \ (S \) ( \ (C [. 1] \) ~ \ (C [I] \) )
for the first \ (i + 1 \) color, can brush \ (C [i + 1] \) times.
And we put it into k blocks, the block has been inserted.
And wherein there are \ (L \) blocks are inserted in the same color adjacent to the middle column.
Thus, transfer equation can be obtained:
\ [F [I +. 1] [J + C [I-kl to +. 1]] + = F [I] [J] * C (J, L) * C (C [ i + 1] -1, k- 1) * C (n-j + 1, kl) \]

Code

#include <cstdio>
#include <cstring>
#define K 16
#define N 110
#define mo 1000000007
#define ll long long
#define mem(a, x) memset(a, x, sizeof a)
#define fo(x, a, b) for (int x = a; x <= b; x++)
#define fd(x, a, b) for (int x = a; x >= b; x--)
using namespace std;
int T, n, m, c[N];
ll f[N][N], jc[N], ny[N];

inline int read()
{
    int x = 0; char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return x;
}

ll ksm(ll x, int y)
{
    ll s = 1;
    while (y)
    {
        if (y & 1) s = s * x % mo;
        x = x * x % mo; y >>= 1;
    }
    return s;
}

ll C(ll x, ll y) {return jc[x] * ny[y] % mo * ny[x - y] % mo;}

int main()
{
    freopen("paint.in", "r", stdin);
    freopen("paint.out", "w", stdout);
    jc[0] = 1; fo(i, 1, 90) jc[i] = jc[i - 1] * i % mo;
    ny[90] = ksm(jc[90], mo - 2);
    fd(i, 89, 0) ny[i] = ny[i + 1] * (i + 1) % mo;
    T = read();
    while (T--)
    {
        m = read(); n = 0;
        fo(i, 1, m) c[i] = read();
        mem(f, 0);
        f[1][c[1] - 1] = 1;
        fo(i, 1, m - 1)
        {
            n += c[i];
            fo(j, 0, n)
                fo(k, 1, c[i + 1])
                    fo(l, 0, (j < k ? j : k))
                        f[i + 1][j + c[i + 1] - k - l] += f[i][j] * C(j, l) % mo * C(c[i + 1] - 1, k - 1) % mo * C(n - j + 1, k - l) % mo;
        }
        printf("%lld\n", f[m][0] % mo);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/jz929/p/11286421.html