Hdoj 4906 Our happy ending pressure DP (digital DP?)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=4906

Test instructions

n number of sequences, each number can choose to fill 0-l, if a sequence can select some number, their and K, then this sequence is a "good sequence", given n<=20,k<=20,0<=l<=10^9, say hello to the number of sequences.

Analysis:

N and K are very small, so try to use this feature (like pressure). Search? )。 Although L is large, but in fact a number greater than k, it is certainly not possible to elect him to form K. Let's consider L<=k's approach first.

Then you can't think of it.

OK, let's see the puzzle.

The current can be found in the problems are relatively simple to write, I slightly more detailed?

Dp[i][j],j is expressed in binary notation, the K-bit is 1, indicating that the sequence of the existing sequence can select several numbers so that the and for the k,dp[i][j] represents the number of the first I can represent the number of schemes of J.

The initial dp[0][0]=1,answer is Σdp[n][j], where the K-bit of J is 1.

Transfer, reverse push a bit difficult to think, or push it, the number of i+1, from 0 to L, we divided into 0,1--k,k+1--l to consider:

If the i+1 bit chooses 0, then Dp[i][j] can be transferred to DP[I+1][J], because there is a 0 more, can be expressed and (j) is the same.

If the i+1 bit chooses the number k+1 to L, then dp[i][j] can be transferred to DP[I+1][J] for the same reason.

If the i+1 bit selects a number from 1 to K, assuming X, then DP[I][J] can be transferred to Dp[i+1][to],to = j|1<< (x-1) | (j<<x) & ((1<<k)-1)

J of the To, we do not choose the current x can be expressed and,1<< (x-1) means only select the current x can be represented and, the last one is already able to represent and add an X, these new and we can express, and in order not to let him exceed the maximum value, So again and the maximum are bitwise with.

And then like a backpack backwards enumeration, you can omit I this dimension.

Code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 Const intMoD = (int) 1e9+7;7 intT, N, K, L, MAX, extra;8 Long Longdp[(1<< -)+Ten];9 intMain ()Ten { Onescanf"%d", &T); A      while(t--) -     { -scanf"%d%d%d", &n, &k, &L); theMemset (DP,0,sizeof(DP)); -         if(L >K) { -Extra = L-K; -L =K; +         } -         ElseExtra =0; +dp[0] =1; AMAX = (1<< K)-1; at          for(inti =1; I <= N; i++) -              for(intj = MAX; J >=0; j--){ -                 if(Dp[j] = =0)Continue; -                 Long LongTMP =Dp[j]; -                  for(intK =1; K <= L; k++){ -                     intto = J |1<< (K-1) | ((j<<k) &MAX); inDp[to] + =tmp; -                     if(Dp[to] >= MoD) dp[to]-=MoD; to                 } +                 //if (dp[j]! = tmp) printf ("%d%lld%lld\n", J, Dp[j], TMP); -DP[J] = (DP[J] + tmp * extra)%MoD; the             } *         Long LongAns =0; $          for(inti =1<< (K-1); I <= MAX; i++){Panax NotoginsengAns + =Dp[i]; -             if(ans >= mod) ans-=MoD; the         } +printf"%i64d\n", ans); A     } the     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.