Ah, this is a very good dynamic planning topic.
At first we did not find the rule, and then the teammate suddenly shouted: "DP Ah!" "Suddenly life suddenly enlightened ...
So she magically launched the transfer equation, and then jointly check the wrong, a out of this problem, really admire this person's high IQ AH
Test instructions
First N, which represents the n person, then A, B, representing the value of two gates respectively.
Then the second line gives the value of the n person, asking you to have all of the n individuals enter the door (either a, B, or one of these doors), in a total of several ways.
But note that the value here is not just added up, it is to add it up to a single digit.
Ideas:
Definition: Dp[i][j] The number of programs that were then obtained in the first person for the value of J
State transfer equation: dp[i][j]=dp[i-1][j]+dp[i-1][j-idx[i] //The meaning here is equal to the number of programs i-1 individuals get J plus i-1 individual get j-idx[i], because here is the equivalent of 01 backpack, We are here at the time of the first person to take the idx[i] this value.
Oh, here's another transfer equation: dp[i][j]=dp[i-1][j]+dp[i-1][j+9-idx[i]] //The meaning here is mainly that the latter is different from the above, the latter means: for example, now your j=2, and then idx= 8, then you also need the extra 3 this number, because (3+8)%9=2, so this is also considered a program number.
The second transfer equation is very important, that is, the pit point of the problem!
Then you need to divide the situation:
1) When everyone goes to B
2) Assign the personnel to A and B, and here it is also the case that all personnel have entered a. We only have to take one dp[n][a] at the end, because the rest of us must go into another B. So the program number is dp[n][a]
3) When the second is not true, then we have to assign people to a. For the convenience of calculation, we separate the second kind of non-establishment into the third situation.
Don't forget the MoD at the end of the pack.
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < vector> #include <set> #include <map> #include <queue> #include <math.h>using namespace std;# Define MAXN 100010#define mod 258280327int idx[maxn];int dp[maxn][12];int main () {int t;int n,a,b;scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &n,&a,&b), memset (idx,0,sizeof (IDX)); memset (Dp,0,sizeof (DP)); int sum=0;for (int i=1;i<=n;i++) {scanf ("%d", &idx[i]); sum+=idx[i];} Sum=sum%9;int Res=0;if (sum==b| | SUM+9==B) res++;if (sum== (a+b) | | (sum+9) = = (A+b) | | (sum+18) = = (A+b)) {dp[0][0]=1;for (int i=1;i<=n;i++) {for (int j=0;j<10;j++) {if (J>=idx[i]) dp[i][j]=dp[i-1][j ]+dp[i-1][j-idx[i]];else Dp[i][j]=dp[i-1][j]+dp[i-1][j+9-idx[i]];dp [I][j]=dp[i][j]%mod;}} printf ("%d\n", (dp[n][a]+res)%mod);} Else{if (sum%a==0) res++;p rintf ("%d\n", res);}}
DP really is very interesting ah!
Keep thinking, come on, come on!!!!!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU (5389)--zero Escape (01 backpack variant)