hdu4906 our happy ending, state compression DP

Source: Internet
Author: User



Test instructions
Give an n number of columns, and take some numbers to form a new series,
If there are some numbers in the new series and K, then this is a good sequence, ask the number of such a series.
N,k<=20, A[i] <=l



Status: Dp[][state] The binary of state in each representation and for (1~k), 1 means it can be taken, and 0 means that it cannot be taken.


State transfer equation:dp[i][state] = SUM (dp[i-1][state ']); State = 1<< (p-1) | |  State ' | | (State ' <<p) &size); 1<=p<=min (k,l); Size= (1<<k)-1;


#include <cstdio> #include <cstring> #include <algorithm>using namespace std;typedef long LL; Const LL mod = 1e9 + 7;    LL Dp[1<<22];int Main () {int T, n, K, L;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n, &k, &l);        int min = min (L, k);        int SIZE = (1<<k)-1;        memset (DP, 0, sizeof DP);        Dp[0] = 1; for (int i=0, i<n; ++i) {for (int j=size; j>=0;--j) if (dp[j]>0) {LL                TMP = Dp[j]; for (int p=1; p<=min; ++p) {int next= (1<< (p-1)) |j| (                    (j<<p) &size);                    Plus P itself this number + originally desirable number + the number of the original desirable to add p after the number, that is composed of all plus p after the case Dp[next] =dp[next] + tmp;                if (dp[next]>mod) dp[next]-= mod;                        {Dp[j] = Dp[j] + (LL) tmp* (l-k) if (k<l)//Number of I is selected greater than K: if (Dp[j]>mod) dp[j]-= mod;        }}} LL ans = 0;            for (int i=0; i<=size; ++i) if (i& (1<< (k-1))) {ans = ans + dp[i];        if (ans>mod) ans-= mod;    } printf ("%i64d\n", ans); } return 0;}


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.