HDU 4906 our happy ending

Source: Internet
Author: User

Test instructions

Given a sequence a_1,a_2,..., A_n, if we can take some of them (each a_i can is only being used once), and they sum to K, then we Say this sequence is a good sequence.
How many good sequence is there? Given that each a_i are an integer and 0<= a_i <= L.

Give you a sequence: A_1, a_2, ..., a_n, if we can take some of them (one per a_i only), and their and is k, we call this sequence a good sequence.

If each a_i is an integer from 0 to L, how many good sequences can they make?

Ideas:

State compression.

Dp[i][s] Represents the length of the sequence of I, can be combined into the sums set of S is the case of how many (set with a number of bits to represent, 1 means can be composed, 0 means not possible. Because the number of useful figures is at most only 20, you can say so.

In this way, we can enumerate the i+1 bits and get a new set of numbers that can be composed. The principle is similar to a backpack. In discussions with others, it was found that the use of bitwise operations can be very convenient to express the contents of the backpack, we assume that the original can be composed of the set is S, now enumeration put in the backpack is the number is J. Then, when not put into the backpack may be composed of a set or S, and put into the backpack, the possible set of the composition becomes (s<<j); so all the collections of enum J may be s| (S<<J) seems to be not very concise.

So, our transfer equation can be written as dp[i+1][s] = Sum{dp[i][s0] | (s0| (S0<<J)) = = S}

It is important to note that the directly opened N*2^n array may burst memory, the observation of the transfer is one layer at a level, so we can use a scrolling array to optimize.

(Finally, thank ALPC students for their patience to explain:)

Code: (This will run for 2 seconds, I really do not know those 0 milliseconds how to do the Orz)

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <string>8#include <queue>9#include <stack>Ten#include <vector> One#include <map> A#include <Set> -#include <functional> -#include <time.h> the  - using namespacestd; -  - typedef __int64 LL; +  - Const intINF =1<< -; + Const intMAXN = +; A Constll MOD = (ll) 1e9+7; at  -ll dp[1<<MAXN]; - intN, K, L; -  - voidsolve () { -Memset (DP,0,sizeof(DP)); in     intMIN =min (L, k); -     intFull = (1<< (k +1))-1;//Complete to  +dp[1] =1; -      for(inti =0; I < n; i++) { the          for(intS = full; S >0; s--)if(dp[s]>0) { *ll tmp =Dp[s]; $              for(intj =1; J <= MIN; J + +)//enumerate each valid numberPanax Notoginsengdp[full& (s| ( S&LT;&LT;J)] = (dp[full& (s| ( S&LT;&LT;J)) (]+tmp)%MOD; -             if(min<L) theDp[s] = (dp[s]+ ((l-min) *tmp)%mod)%MOD; +         } A     } the  +ll ans =0; -      for(intS =1; S <= full; s++)if(s& (1<<(k))) $Ans = (ans+dp[s])%MOD; $  -printf"%i64d\n", ans); - } the  - intMain () {Wuyi #ifdef PHANTOM01 theFreopen ("HDU4906.txt","R", stdin); -     #endif //Phantom01 Wu  -     intT; Aboutscanf"%d", &T); $      while(t--) { -scanf"%d%d%d", &n, &k, &L); - solve (); -     } A  +     return 0; the}
View Code

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.