hdoj4906 our happy Ending (multi-university Training Contest 4)

Source: Internet
Author: User

For a fixed-length (size = N) sequence A, if there is a subset of "positional correlation" (with an empty set) that makes the sum of all the elements of the subset K, then the sequence A is counted.

Where the sequence a of any element a[i] in [0, L] free to take the value.

Data conditions 0≤n, k≤20, 0≤L≤1E9, count results for mod = 1e9 + 7 modulo.

No matter the direct count or the consideration from the negative count can not solve the problem of heavy, can only consider DP.

Enumerates the length of the sequence I and the compressed state J, and records the number of sequence selection schemes under this condition DP[I][J].

The compressed State J indicates the selection of the collection {1, 2, ..., Min (l, K)}.

Where the first element of the set is in the state J, and only if the first bit of the binary string J is 1.

Obviously we have dp[0][0] = 1.

For an array of length p, the element that can be selected at p bit is 0,1,2,...,l

Consider P-bit selection 1,2,...,min (L, K)

Then for the any state J when the array length is p-1, the state after appending the element I to the array is:

J1 = J | ((J << i) & ((1 << k)-1)) | (1 << (i-1))

The three parts represent the original state, the state of each element in the original state and the increment of the element I, I itself.

So dp[p][j1] + = Dp[p-1][j].

For P-bit selection 0 or greater than min (L, k), J1 = J.

Therefore there is dp[p][j] = (L-min (l, k)) * Dp[p-1][j].

We finally only need to dp[n][j], where J K bit is 1 cumulative.

Because each element of the sequence is non-negative, we finally only care about those sets that are present and a subset of K,

And those elements greater than k are necessarily not elements that make up a subset, only those elements that are less than k are useful for state transitions.

So we can represent states like this.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <map>5#include <string>6#include <vector>7#include <Set>8#include <cmath>9#include <ctime>Ten using namespacestd; One #defineLson (U << 1) A #defineRson (U << 1 | 1) - typedef __int64 LL; - Const intMAXN = 1e6 +Ten; the Const intMAXM =1050; - Constll mod = 1e9 +7; -  - intN, l, K; +ll dp[(1<< +) +Ten]; - intMain () { +     intT; Ascanf"%d", &T); at      while(t--){ -scanf"%d%d%d", &n, &k, &l); -ll d = ABS (L-k); -L =min (l, k); -         ints = (1<< k)-1; -Memset (DP,0,sizeofDP); indp[0] =1; -          while(n--){ to              for(intj = S; J >=0; j--){ +ll tem =Dp[j]; -                 if(!tem)Continue; the                  for(intp =1; P <= l; p++){ *                     intNEX = (1<< (P-1)) | J | (j << P) &s); $Dp[nex] = (Dp[nex] + tem)%MoD;Panax Notoginseng                 } -DP[J] = (TEM * (1+ D))%MoD; the             } +         } All ans =0; the          for(inti = s; I >=0; i--) +             if(I & (1<< (K-1))) -Ans = (ans + dp[i])%MoD; $printf"%i64d\n", ans); $     } -     return 0; -}
View Code

hdoj4906 our happy Ending (multi-university Training Contest 4)

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.