[Hiho 09] State compression

Source: Internet
Author: User

Title Description

Sum[i, J, S] indicates that the first J position of line I is previously (1th to line I, row 1th to column j–1) has been fully filled, and the number of feasible schemes to fill the remaining position under the constraints of State S. S is a compressed representation of the I and I + 1 row cake placement status, SJ = (1 << (j-1)) & S,s The lowest significant bit of the 1th position of the first row of the case, the most significant bit represents the first I + 1 row m position, 1 means filled, 0 Indicates no padding.

The state transition equation is as follows:

(The second state of the figure should be divided into two, if i = N, then equal to 1, otherwise it is shown in the figure).

The branch of the equation is very complex to write, in fact, the main look is three conditions:

1. Whether the current position (I, j) has been filled;

2. Whether the current position has a right (I, J + 1), if any, the right side has been filled;

3. If the current position is not filled can fill the current position and its right current position has the lower side (i + 1, j), if so, whether the lower side has been filled.

These three conditions are to determine whether the current position needs to be populated, if so, and how it can be populated.

The final result is sum[1, 1, 0].

#include <iostream> #include <algorithm>using namespace std;int sum[1005][6][1 << 10];int N, m;int main () {cin >> n >> m;for (int i = n; I >= 1; i--) {for (int j = m; j >= 1; j--) {for (int k = (1 << ( 2 * m))-1; K >= 0; k--) {if ((1 << (j-1)) & k) {if (j = = m) {if (I < n) {Sum[i][j][k] = sum[i + 1][1][k >> m];} else {sum[i][j][k] = 1;}} else {Sum[i][j][k] = sum[i][j + 1][k];}} else {bool R_set = ((j = = m) | | ((1 << (j)) & K)); BOOL D_set = ((i = = N) | | ((1 << (j + m-1)) & K)); if (R_set) {if (D_set) {sum[i][j][k] = 0;} else {Sum[i][j][k] = sum[i][j][k + (1 << (j-1)) + (1 << (M + j-1))];}} else {if (D_set) {Sum[i][j][k] = sum[i][j][k + (1 << (j-1)) + (1 << j)];} else {Sum[i][j][k] = sum[i][j][k + (1 << (j-1)) + (1 << (M + j-1))]+ sum[i][j][k + (1 << (j-1)) + (1 << j)];}} SUM[I][J][K]%= 1000000007;}}} cout << sum[1][1][0] << Endl;return0;} 

[Hiho 09] State compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.