0-1 backpack modified version, 0-1 backpack modified version

Source: Internet
Author: User

0-1 backpack modified version, 0-1 backpack modified version
Description

I spent the money, so I am single. I am single. So I spent the money on Double 11 and Double 11.

This year, Nova June (No. 3) still lived his "shopping and buying" double "11", but it was not so self-willed because of shame. His shopping cart is filled with countless items. There are N items in total, and there are more than one item _ (: 3 "items) _ now, Nova wants to make a difficult decision. He wants to pick out m pieces from all the products and combine them into an order. How many methods can be used to make a single order? Returns the remainder of M.

PS: the same product is not differentiated.

Input

Multiple groups of test data (no more than 100 groups)

There are two rows of data in each group. The first row has three positive integers: N, m, and M. For details, see description. The second row has N positive integers: a1, a2,, represents the number of products I

(1 <= N, ai, m <= 1000,2 <= M <= 10000)

Output

Output a row for each group of data, indicating the total number of methods

Input example
3 3 100001 2 3
Output example
6

Source of http://biancheng.love/contest/17/problem/G/index
For details, see the code and comments:
1 # include <bits/stdc ++. h> 2 3 using namespace std; 4 const int N = 1010; 5 int a [N]; 6 int B [N] [N]; 7 int main () 8 {9 int n, m, M; 10 while (scanf ("% d", & n, & m, & M) = 3) 11 {12 for (int I = 0; I <n; I ++) 13 scanf ("% d", & a [I]); // The number of items for each product is 14 15 for (int I = 0; I <= n; I ++) 16 B [I] [0] = 1; 17 18 for (int I = 0; I <n; I ++) // 19 for (int j = 1; j <= m; j ++) // pick out m pieces 20 {21 if (j-1-a [I]> = 0) // whether the number of items in I is greater than 22 B [I + 1] [j] = (B [I] [j] + B [I + 1] [J-1]-B [I] [j-1-a [I] + M) % M; 23 else24 B [I + 1] [j] = (B [I] [j] + B [I + 1] [J-1]) % M; 25} 26 printf ("% d \ n", B [n] [m]); 27} 28 29}
Some template functions of the backpack are attached:
1 void ZoreOnePack (int cost, int weight) // 0-1 backpack 2 {3 for (int I = W; I> = weight; -- I) 4 f [I] = max (f [I], f [I-weight] + cost); 5} 6 7 void CompletePack (int cost, int weight) // complete backpack 8 {9 for (int I = weight; I <= W; ++ I) 10 f [I] = max (f [I], f [I-weight] + cost); 11} 12 13 void MultiPack (int cost, int weight, int num) // multiple backpacks 14 {15 if (num * weight> = W) 16 CompletePack (cost, weight); 17 else18 {19 int k = 1; 20 while (k <num) 21 {22 ZoreOnePack (cost * k, weight * k); 23 num-= k; 24 k + = k; 25} 26 ZoreOnePack (cost * num, weight * num ); 27} 28} 29 30 void TwoZoreOnePack (int cost, int weight, int weight) 31 {32 for (int I = W; I >= weight; -- I) 33 for (int j = M; j> = comment; -- j) 34 two [I] [j] = max (two [I] [j], two [I-weight] [j-weight] + cost); 35} 36 37 void TwoCompletePack (int cost, int weight, int weight) 38 {39 for (int I = weight; I <= W; ++ I) 40 for (int j = weight; j <= W; ++ j) 41 two [I] [j] = max (two [I] [j], two [I-weight] [j-weight] + cost ); 42} 43 44 void TwoMultiPack (int cost, int weight, int weight, int num) 45 {46 if (num * weight> = W & num * weight> = M) 47 TwoCompletePack (cost, weight, weight); 48 else49 {50 int k = 1; 51 while (k <num) 52 {53 TwoZoreOnePack (cost * k, weight * k, optional * k); 54 num-= k; 55 k + = k; 56} 57 TwoZoreOnePack (cost * num, weight * num, interval * num); 58} 59}
 

 



 

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.