Our happy ending

Source: Internet
Author: User

Topic links

    • Test Instructions:
      Enter N, K, l,n number, maximum value not exceeding L, number of numbers in sequence and number of sequences that can reach K
      n,k<=20, 0<=l<=10^9
    • Analysis:
      The key is that it is smaller than the K, so you can consider DP.
      Talk about the DP state you think of when you play. It was a long time before I found fault.

      ...

      Dp[i][j] Indicates the number of first I in the sequence (must be selected), and the number of sequences that can fetch J.

      The problem with this state is that it is repeated: for a definite sequence, because of the ability to pick some numbers to sum, so for dp[i], the same sequence can get very many different and J, that is, it has been calculated many times to cause repetition.
      Compare one of the similar questions you've seen before, given a sequence. Ask for a number of numbers can be reached and K scheme number: Dp[i][j] is the current sequence of the number of I (Must be selected), and the number of sequences can be taken to J, the problem is that this is correct.
      Why is this? It is because the answer is inferred differently: the answer to the first question infers that the current sequence hypothesis can fetch a number of numbers so that it is K. So the answer is plus one; the second problem is that there are X schemes. Takes a number of numbers from the current sequence to make and K. Then the answer plus X. Other words. The first question is inferred based on whether the sequence is satisfied, and the second is whether the collection is satisfied. The DP scheme is in fact selecting the elements in the collection, because dp[i][j] means I must be selected, that is, in the collection.

      So, how do you infer if a sequence is satisfied: The value of the current position of the sequence is enumerated from left to right, so that if the current sequence is satisfied, that is, if the sum can go to K, then it is necessary to record who the current sequence can reach and who it is. So far. You can use the pressure DP to solve it. Describe the meaning of the binary representation: The first 1 represents and is 1. The second 1 represents and is 2 ...
      reflect on the program when writing the problem: no practical scrolling array, resulting in the wrong many times.

      Because the state transfer of this topic simply shifts to a larger state value (and can be itself). So we can not adopt the rolling array, one-dimensional solution.

      However, this requires additional attention to a problem. themselves to the problem of being transferred to themselves. Because it's not very good to understand. Cause the bug has been not found. Study.

const int MAXN = 21;int dp[1 << maxn];int main () {int T, n, Sum, Max;    RI (T);        FE (Kase, 1, T) {RIII (n, Sum, Max);        int min = min (sum, Max);        int all = 1 << sum; CLR (DP, 0);        Dp[0] = 1;                REP (i, N) {FED (J, all-1, 0) {if (dp[j] = = 0) continue;                int x = Dp[j]; for (int k = 1; k <= Min; k++) {int NXT = j | ((J << k) & (ALL-1)) |                    (1 << (k-1));                    DP[NXT] + = x;                if (DP[NXT] >= MoD) dp[nxt]-= mod;            } if (Max-min > 0) dp[j] = (Dp[j] + 1LL * (max-min) * x)% MOD;        }} int ans = 0;            FF (i, 1 << (sum-1), all) {ans + = dp[i];        if (ans >= mod) ans-= mod;    } WI (ANS); } return 0;}


Using dp[1] to represent a 0 approach is actually not good. Since the meaning of 3 (11) and 2 (10) is the same, and does not contain or include 0 (first one) is meaningless
const int MAXN = 21;int dp[1 << maxn];int main () {int T, n, Sum, Max;    RI (T);        FE (Kase, 1, T) {RIII (n, Sum, Max);        int min = min (sum, Max);        int all = 1 << (sum + 1); CLR (DP, 0);        DP[1] = 1;                REP (i, N) {FED (J, all-1, 1) {if (dp[j] = = 0) continue;                int x = Dp[j]; for (int k = 1; k <= Min; k++) {int NXT = j |                    ((J << k) & (ALL-1));                    DP[NXT] + = x;                if (DP[NXT] >= MoD) dp[nxt]-= mod;            } if (Max-min > 0) dp[j] = (Dp[j] + 1LL * (max-min) * x)% MOD;        }} int ans = 0;            FF (i, 1 << sum, all) {ans + = dp[i];        if (ans >= mod) ans-= mod;    } WI (ANS); } return 0;}



Our happy ending

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.