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 problem is that the key is smaller than k, so you can consider DP.
first say the time when I think of the DP state of the game, a long time to find the wrong .... DP[I][J] Indicates the number of first I (must be selected) in the sequence, and the number of sequences that can fetch J. The problem with this state is the repetition: for a definite sequence, because you can pick some numbers to sum up, for Dp[i], the same sequence can get a lot of different and J, that is, it has been calculated many times to cause repetition.
Compare to a similar question previously seen, given a sequence, ask a number of numbers can reach and K scheme number: Dp[i][j] indicates that the current sequence of the number of I (Must be selected), and the number of sequences can be taken to J, the problem so that is correct.
Why is this? It is because the answer is different: the answer to the first question is that if the current sequence can fetch a number of numbers and is k, then the answer is plus one; the second problem is that if there are X schemes, take a number of numbers from the current sequence and k, then the answer plus X. In other words, the first question is based on whether the sequence is satisfied, and the second is whether the collection is satisfied. The DP scheme is actually selecting the elements in the collection, because dp[i][j] means I must be selected, that is, in the collection.
So, how to determine whether a sequence is satisfied: From left to right to enumerate the values of the current position of the sequence, then ask whether the current sequence is satisfied, that is, whether the sum can be to K, then it is necessary to record the current sequence can reach and who, so far, you can use the pressure DP to solve. Describe the meaning of the binary representation: The first 1 represents and is 0 (so the main transfer is convenient, in fact, can be 0 and 0), the second 1 represents and 1 ...
reflect on the problem when the program is written: no scrolling array, resulting in many mistakes. Since the state transfer of this topic will only be transferred to a larger state value (or it can be itself), it can be resolved without scrolling an array. But this needs to pay extra attention to a problem, self-transfer to their own problems, because the understanding here is not very good, leading to always find out bugs, learning.
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;}