@ Noi.ac - 508 @ 01 backpack


@description@

One day you learned algorithms to solve a 01 knapsack problem, you decide to apply this algorithm to the NOI game.

Do you have a backpack the size of V.

There are n items, each item has a m. Each item has a volume for the i-th item of the j-th, its volume is vij.

You want a certain number of articles sequentially into bags, it requires the use of only one article each time and can not exceed the total volume V, except that the article is not adjacent to claim isotype. You want to know how many kinds of programs.

Different two solutions if and only if the two different sets of selected items, the same or a different collection of items but the order of these items. Two identical volume of the same kind of items counted as different items.

You can select all the items can not choose an item.

The total value of the output digital to analog program 10 ^ 9 + 7.

input
of the first row of three integers n, m, V, meaning as the title.

Next n lines of m integers, wherein a first number of i-th row j to the value vij.

output
line of output an integer value representing the number of mold 10 ^ 7 + 9 overall scheme.

sample input
2 2 3
1 2
2 2
sample output
9

explanation
we first two articles "a" and "b" in lowercase letters, two second article in uppercase letters "A" and "B". Wherein a volume of only 1, three other items are volume 2.

Nine kinds of solutions are: not selected, a, b, A, B, aA, aB, Aa, and Ba.

Note that AB is not a valid solution because their total volume of 2 + 2 = 4, greater than 3.

ab is not a valid solution because although their total volume does not exceed 3, with two kinds of goods (ie a and b) adjacent to this scenario.

To 100% of the data, 1≤n, m≤50,1≤vij, V≤100.

@solution@

If consideration has been given the number of each item respectively, so that how to obtain the same type of article the total number of non-adjacent alignment.

The most common combination is not adjacent to the article counting method for an insertion space law, i.e. to the planned position of the article and then into the other articles in the voids of other articles.
But because of the requirements of each kind of items they are adjacent to each other, so that this method can not be applied. Because there may be adjacent to the current items, certain items will be added later they opened the barrier.

Another way to consider: bundling a plurality of articles (i.e., so that these items must together) and inclusion and exclusion.
So that a [1 ... k] represents one bundle embodiment, wherein a [i] denotes the i-th binding to a [i] a group of articles into a bundle. Can be found by searching all legal a.
Tolerance can be obtained by manually repellent derived formula:
\ [ANS = \ sum_k (K * \ prod_ {I} = ^ {K}. 1 ((-. 1) ^ {A [I] -1} * (A [! i]!))) \]

Wherein \ (! A [i] \ ) represents the number of programs within each group (i.e. full internal arrangement), \ ((-. 1) ^ {A [I] -1} \) is the coefficient of inclusion and exclusion, \ (k! \) that is arranged after the whole bundle.

But obviously this algorithm is unqualified.
We still use the inclusion and exclusion + bundled with the idea, consider changing the direction of thinking, that is not to enumerate the number of each item separately.
The final answer can be found only with the number of groups related to the number of items in each group after bundling and bundled. So we can set the number of items bundle obtained as a new item, the new item while giving weight values (i.e., the above formula \ ((- 1) ^ { a [i] -1} * (a [i ]!) \) ).
Then for all items in a backpack with the right value dp, and finally represented by a full factorial arrangement (i.e. the formula \ (K! \) ).

However, this algorithm has a little flaw: if we put items 1, 2 combined into one item, items 2, 3 into one article, not difficult to find these two new items can not coexist.
But since we only put the same kind of items together, so we can then through a dp avoid this problem.

Definition of DP [i] [j] [V] denotes the i-th article is divided into j groups, accounting for weights and volume V of the backpack.
If the addition of a new item, or it is a separate group; or it is added in front of a certain group, each subsequent time prior to the addition of this item has a position, and the top of each group also has a location, so a total of i + j positions available (similar to the first type Stirling number, but the number of first type Stirling a circular arrangement and general arrangement here). At the same time added to the front of the group to go, the group size is changed, the coefficients to be multiplied by -1-repellent capacity.
Therefore, to obtain transfer of the formula:
\ [DP [I +. 1] [J] [V] = DP [I] [J-. 1] [Vv] - DP [I] [J] [Vv] * (I + J) \ ]

Wherein v is the volume of the newly added items.

All items are noted in the title having a volume, so that the upper bound of the capacity of the backpack number of articles can be loaded backpack.
Last enumerated backpack volume occupied by the items O (V), enumeration number of items that has the Backpacks O (V), the kind of articles enumerated O (n), accounting for the enumeration of such articles knapsack capacity O (V), how many such items enumerated groups added backpack O (m).
The total time complexity O (V ^. 3 n- m). Enumeration sequence information can be exchanged to such items is determined, and then determines whether the program number information is 0, if 0 is skipped.
This pruning effect is good enough to allow you ran this question, at the time complexity of such a theory unscientific.

@accepted code@

#include<cstdio>
const int MOD = int(1E9) + 7;
int f[50 + 5][50 + 5][100 + 5];
int dp[50 + 5][100 + 5][100 + 5], g[50 + 5][100 + 5];
int main() {
    int n, m, v, V; scanf("%d%d%d", &n, &m, &V);
    dp[0][0][0] = 1;
    for(int i=1;i<=n;i++) {
        for(int j=0;j<=m;j++)
            for(int k=0;k<=j;k++)
                for(int l=0;l<=V;l++)
                    f[j][k][l] = 0;
        f[0][0][0] = 1;
        for(int j=1;j<=m;j++) {
            scanf("%d", &v);
            for(int p=j;p>=1;p--) {
                for(int q=p-1;q>=1;q--)
                    for(int k=V;k>=v;k--)
                        f[p][q][k] = (f[p][q][k] + (f[p-1][q-1][k-v] + 1LL*(MOD - 1)*(p + q - 1)%MOD*f[p-1][q][k-v]%MOD)%MOD)%MOD;
                for(int k=V;k>=v;k--)
                    f[p][p][k] = (f[p][p][k] + f[p-1][p-1][k-v])%MOD;
            }
        }
        for(int j=0;j<=m;j++)
            for(int k=0;k<=V;k++) {
                g[j][k] = 0;
                for(int l=j;l<=m;l++)
                    g[j][k] = (g[j][k] + f[l][j][k])%MOD;
            }
        for(int j=0;j<=m;j++)
            for(int k=0;k<=V;k++) {
                if( !g[j][k] ) continue;
                for(int p=j;p<=V;p++)
                    for(int q=k;q<=V;q++)
                        dp[i][p][q] = (dp[i][p][q] + 1LL*dp[i-1][p-j][q-k]*g[j][k])%MOD;
            }
    }
    int ans = 0;
    for(int i=0,f=1;i<=V;i++,f=1LL*f*i%MOD)
        for(int j=0;j<=V;j++)
            ans = (ans + 1LL*f*dp[n][i][j]%MOD)%MOD;
    printf("%d\n", ans);
}

@details@

This is regarded as the more interesting question a few days experiencing.

Once again confirms, inclusion and exclusion in itself is difficult to think. . .
For example, this year on that road THUWC inclusion and exclusion problems. . .

Time and complexity required to run clearly calculated 2.5 * 10 ^ 9 was able to run fast in the case of pruning. . .

Guess you like

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